diff --git a/angular-css-injector.js b/angular-css-injector.js index 69fa027..ac80057 100644 --- a/angular-css-injector.js +++ b/angular-css-injector.js @@ -9,20 +9,19 @@ angular.module('angular.css.injector', []) .provider('cssInjector', function() { var singlePageMode = false; - + function CssInjector($compile, $rootScope){ // Variables - var singlePageMode = false, - head = angular.element(document.getElementsByTagName('head')[0]), - scope; - + var head = angular.element(document.getElementsByTagName('head')[0]), + scope; + // Capture the event `locationChangeStart` when the url change. If singlePageMode===TRUE, call the function `removeAll` $rootScope.$on('$locationChangeStart', function() { if(singlePageMode === true) removeAll(); }); - + // Always called by the functions `addStylesheet` and `removeAll` to initialize the variable `scope` var _initScope = function() { @@ -32,12 +31,12 @@ angular.module('angular.css.injector', []) throw("angular.css.injector error : Please initialize your app in the HTML tag and be sure your page has a HEAD tag."); } }; - + // Used to add a CSS files in the head tag of the page var addStylesheet = function(href) { _initScope(); - + if(scope.injectedStylesheets === undefined) { scope.injectedStylesheets = []; @@ -51,29 +50,43 @@ angular.module('angular.css.injector', []) return; } } - + scope.injectedStylesheets.push({href: href}); }; - + + var remove = function(href){ + _initScope(); + + if(scope.injectedStylesheets){ + for(var i = 0; i < scope.injectedStylesheets.length; i++){ + if(scope.injectedStylesheets[i].href === href){ + scope.injectedStylesheets.splice(i, 1); + return; + } + } + } + }; + // Used to remove all of the CSS files added with the function `addStylesheet` var removeAll = function() { _initScope(); - + if(scope.injectedStylesheets !== undefined) scope.injectedStylesheets = []; // Make it empty }; - + return { add: addStylesheet, + remove: remove, removeAll: removeAll }; } - + this.$get = function($compile, $rootScope){ return new CssInjector($compile, $rootScope); }; - + this.setSinglePageMode = function(mode){ singlePageMode = mode; return this;