99angular . module ( 'angular.css.injector' , [ ] )
1010. provider ( 'cssInjector' , function ( ) {
1111 var singlePageMode = false ;
12-
12+
1313 function CssInjector ( $compile , $rootScope ) {
1414 // Variables
15- var singlePageMode = false ,
16- head = angular . element ( document . getElementsByTagName ( 'head' ) [ 0 ] ) ,
17- scope ;
18-
15+ var head = angular . element ( document . getElementsByTagName ( 'head' ) [ 0 ] ) ,
16+ scope ;
17+
1918 // Capture the event `locationChangeStart` when the url change. If singlePageMode===TRUE, call the function `removeAll`
2019 $rootScope . $on ( '$locationChangeStart' , function ( )
2120 {
2221 if ( singlePageMode === true )
2322 removeAll ( ) ;
2423 } ) ;
25-
24+
2625 // Always called by the functions `addStylesheet` and `removeAll` to initialize the variable `scope`
2726 var _initScope = function ( )
2827 {
@@ -32,12 +31,12 @@ angular.module('angular.css.injector', [])
3231 throw ( "angular.css.injector error : Please initialize your app in the HTML tag and be sure your page has a HEAD tag." ) ;
3332 }
3433 } ;
35-
34+
3635 // Used to add a CSS files in the head tag of the page
3736 var addStylesheet = function ( href )
3837 {
3938 _initScope ( ) ;
40-
39+
4140 if ( scope . injectedStylesheets === undefined )
4241 {
4342 scope . injectedStylesheets = [ ] ;
@@ -51,29 +50,43 @@ angular.module('angular.css.injector', [])
5150 return ;
5251 }
5352 }
54-
53+
5554 scope . injectedStylesheets . push ( { href : href } ) ;
5655 } ;
57-
56+
57+ var remove = function ( href ) {
58+ _initScope ( ) ;
59+
60+ if ( scope . injectedStylesheets ) {
61+ for ( var i = 0 ; i < scope . injectedStylesheets . length ; i ++ ) {
62+ if ( scope . injectedStylesheets [ i ] . href === href ) {
63+ scope . injectedStylesheets . splice ( i , 1 ) ;
64+ return ;
65+ }
66+ }
67+ }
68+ } ;
69+
5870 // Used to remove all of the CSS files added with the function `addStylesheet`
5971 var removeAll = function ( )
6072 {
6173 _initScope ( ) ;
62-
74+
6375 if ( scope . injectedStylesheets !== undefined )
6476 scope . injectedStylesheets = [ ] ; // Make it empty
6577 } ;
66-
78+
6779 return {
6880 add : addStylesheet ,
81+ remove : remove ,
6982 removeAll : removeAll
7083 } ;
7184 }
72-
85+
7386 this . $get = function ( $compile , $rootScope ) {
7487 return new CssInjector ( $compile , $rootScope ) ;
7588 } ;
76-
89+
7790 this . setSinglePageMode = function ( mode ) {
7891 singlePageMode = mode ;
7992 return this ;
0 commit comments