-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathroute-scripts.js
More file actions
41 lines (38 loc) · 1.44 KB
/
route-scripts.js
File metadata and controls
41 lines (38 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* Created by Victor Avendano on 1/10/15.
* avenda@gmail.com
*/
'use strict';
(function(){
var mod = angular.module('routeScripts', ['ngRoute']);
mod.directive('routeScripts', ['$rootScope','$compile',
function($rootScope, $compile){
return {
restrict: 'E',
link: function (scope, element) {
var html = '<script ng-src="{{script}}" ng-repeat="script in routeScripts"></script>';
element.append($compile(html)(scope));
scope.routeScripts = [];
$rootScope.$on('$routeChangeStart', function (e, next, current) {
if(current && current.$$route && current.$$route.js){
if(!Array.isArray(current.$$route.js)){
current.$$route.js = [current.$$route.js];
}
current.$$route.js.forEach(function(script, index){
scope.routeScripts.splice(index, 1);
});
}
if(next && next.$$route && next.$$route.js){
if(!Array.isArray(next.$$route.js)){
next.$$route.js = [next.$$route.js];
}
next.$$route.js.forEach(function(script, index){
scope.routeScripts.push(script);
});
}
});
}
};
}
]);
})();