9
9
angular . module ( 'angular.css.injector' , [ ] )
10
10
. provider ( 'cssInjector' , function ( ) {
11
11
var singlePageMode = false ;
12
-
12
+
13
13
function CssInjector ( $compile , $rootScope ) {
14
14
// 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
+
19
18
// Capture the event `locationChangeStart` when the url change. If singlePageMode===TRUE, call the function `removeAll`
20
19
$rootScope . $on ( '$locationChangeStart' , function ( )
21
20
{
22
21
if ( singlePageMode === true )
23
22
removeAll ( ) ;
24
23
} ) ;
25
-
24
+
26
25
// Always called by the functions `addStylesheet` and `removeAll` to initialize the variable `scope`
27
26
var _initScope = function ( )
28
27
{
@@ -32,12 +31,12 @@ angular.module('angular.css.injector', [])
32
31
throw ( "angular.css.injector error : Please initialize your app in the HTML tag and be sure your page has a HEAD tag." ) ;
33
32
}
34
33
} ;
35
-
34
+
36
35
// Used to add a CSS files in the head tag of the page
37
36
var addStylesheet = function ( href )
38
37
{
39
38
_initScope ( ) ;
40
-
39
+
41
40
if ( scope . injectedStylesheets === undefined )
42
41
{
43
42
scope . injectedStylesheets = [ ] ;
@@ -51,29 +50,43 @@ angular.module('angular.css.injector', [])
51
50
return ;
52
51
}
53
52
}
54
-
53
+
55
54
scope . injectedStylesheets . push ( { href : href } ) ;
56
55
} ;
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
+
58
70
// Used to remove all of the CSS files added with the function `addStylesheet`
59
71
var removeAll = function ( )
60
72
{
61
73
_initScope ( ) ;
62
-
74
+
63
75
if ( scope . injectedStylesheets !== undefined )
64
76
scope . injectedStylesheets = [ ] ; // Make it empty
65
77
} ;
66
-
78
+
67
79
return {
68
80
add : addStylesheet ,
81
+ remove : remove ,
69
82
removeAll : removeAll
70
83
} ;
71
84
}
72
-
85
+
73
86
this . $get = function ( $compile , $rootScope ) {
74
87
return new CssInjector ( $compile , $rootScope ) ;
75
88
} ;
76
-
89
+
77
90
this . setSinglePageMode = function ( mode ) {
78
91
singlePageMode = mode ;
79
92
return this ;
0 commit comments