Skip to content
This repository was archived by the owner on Feb 8, 2023. It is now read-only.

Commit b1a6fb2

Browse files
Gabriel DelépineGabriel Delépine
Gabriel Delépine
authored and
Gabriel Delépine
committed
Rename module and service
1 parent 5510051 commit b1a6fb2

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

angular-css-injector.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
/*
2-
* angularDynamicStylesheets v0.2.2
3-
* Copyleft 2013 Yappli
2+
* angular-css-injector v0.2.2
3+
* Written by Gabriel Delépine
44
* License: MIT
5-
* https://github.com/Yappli/angularDynamicStylesheets/
5+
* https://github.com/Yappli/angular-css-injector/
66
*/
7-
angular.module('DynamicStylesheets', [])
8-
.service('dynamicStylesheets', [
7+
angular.module('angular.css.injector', [])
8+
.service('cssInjector', [
99
'$compile',
1010
'$rootScope',
1111
function($compile, $rootScope)
1212
{
1313
// Variables
14-
var scope;
15-
var singlePageMode = false;
14+
var singlePageMode = false,
15+
head = angular.element(document.querySelector('head')), // TO make the code IE < 8 compatible, include jQuery in your page and replace "angular.element(document.querySelector('head'))" by "angular.element('head')"
16+
scope;
1617

1718
// Capture the event `locationChangeStart` when the url change. If singlePageMode===TRUE, call the function `removeAll`
18-
$rootScope.$on('$viewContentLoaded', function()
19+
$rootScope.$on('$locationChangeStart', function()
1920
{
2021
if(singlePageMode === true)
2122
removeAll();
@@ -25,45 +26,45 @@ angular.module('DynamicStylesheets', [])
2526
var _initScope = function()
2627
{
2728
if(scope === undefined)
28-
scope = angular.element(document.querySelector('head')).scope();
29+
scope = head.scope(); // We initialise head's scope in a separated function because it is not defined at the time of the initialisation of the service.
2930
};
3031

3132
// Used to add a CSS files in the head tag of the page
3233
var addStylesheet = function(href)
3334
{
3435
_initScope();
3536

36-
if(scope.href_array_dynamicStylesheets === undefined)
37+
if(scope.injectedStylesheets === undefined)
3738
{
38-
scope.href_array_dynamicStylesheets = [];
39-
angular.element(document.querySelector('head')).append($compile("<link data-ng-repeat='stylesheet in href_array_dynamicStylesheets' data-ng-href='{{stylesheet.href}}' rel='stylesheet' />")(scope)); // Found here : http://stackoverflow.com/a/11913182/1662766
39+
scope.injectedStylesheets = [];
40+
head.append($compile("<link data-ng-repeat='stylesheet in injectedStylesheets' data-ng-href='{{stylesheet.href}}' rel='stylesheet' />")(scope)); // Found here : http://stackoverflow.com/a/11913182/1662766
4041
}
4142
else
4243
{
43-
for(var i in scope.href_array_dynamicStylesheets)
44+
for(var i in scope.injectedStylesheets)
4445
{
45-
if(scope.href_array_dynamicStylesheets[i].href == href) // An url can't be added more than once. I use a loop FOR, not the function indexOf to make IE < 9 compatible
46+
if(scope.injectedStylesheets[i].href == href) // An url can't be added more than once. I use a loop FOR, not the function indexOf to make the code IE < 9 compatible
4647
return;
4748
}
4849
}
4950

50-
scope.href_array_dynamicStylesheets.push({href: href});
51+
scope.injectedStylesheets.push({href: href});
5152
};
5253

5354
// Used to remove all of the CSS files added with the function `addStylesheet`
5455
var removeAll = function()
5556
{
5657
_initScope();
5758

58-
if(scope.href_array_dynamicStylesheets !== undefined)
59-
scope.href_array_dynamicStylesheets = []; // Make it empty
59+
if(scope.injectedStylesheets !== undefined)
60+
scope.injectedStylesheets = []; // Make it empty
6061
};
6162

6263
// Used to set the boolean `singlePageMode`. If singlePageMode===TRUE, the function `removeAll` will be call every time the page change (based on the angular event `$locationChangeStart`)
6364
var setSinglePageMode = function(bool)
6465
{
6566
if(bool !== true && bool !== false)
66-
throw("Angular service `dynamicStylesheets` : function `setSinglePageMode` : Error parameter, boolean required.");
67+
throw("Angular service `cssInjector` : function `setSinglePageMode` : Error parameter, boolean required.");
6768

6869
singlePageMode = bool;
6970
};

0 commit comments

Comments
 (0)