forked from tennisgent/angular-route-styles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui-route-styles.js
55 lines (48 loc) · 1.41 KB
/
ui-route-styles.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* Created by Zack Boman on 1/31/14.
* UI Router version by Jorick van Hees on 2/27/14.
* http://www.zackboman.com or tennisgent@gmail.com
*/
(function(){
var mod = angular.module('routeStyles', ['ui.router']);
mod.directive('head', ['$rootScope','$compile',
function($rootScope, $compile){
return {
restrict: 'E',
link: function(scope, elem){
var html = '<link rel="stylesheet" ng-repeat="(routeCtrl, cssUrl) in routeStyles" ng-href="{{cssUrl}}" >';
elem.append($compile(html)(scope));
scope.routeStyles = {};
$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
// Remove old styles
if(fromState && fromState.views){
angular.forEach(fromState.views, function(view){
if(view.css){
if(!Array.isArray(view.css)){
view.css = [view.css];
}
angular.forEach(view.css, function(sheet){
scope.routeStyles[sheet] = undefined;
});
}
});
}
// Add new styles
if(toState && toState.views){
angular.forEach(toState.views, function(view){
if(view.css){
if(!Array.isArray(view.css)){
view.css = [view.css];
}
angular.forEach(view.css, function(sheet){
scope.routeStyles[sheet] = sheet;
});
}
});
}
});
}
};
}
]);
})();