CSS on-demand for AngularJS
Optimize the presentation layer of your single-page apps by dynamically injecting stylesheets as needed.
AngularCSS listens for route (or states) change events, adds the CSS defined on the current route and removes the CSS from the previous route. It also works with directives in the same fashion with the compile and scope destroy events. See the code samples below for more details.
Read the Article
Introducing AngularCSS: CSS On-Demand for AngularJS
Angular's ngRoute Demo (source)
Install and manage with Bower or jspm. A CDN is also provided by cdnjs.com
$ bower install angular-css
$ jspm install github:castillo-io/angular-css
index.html(ngRoute and UI Router are optional).
angularCSSas a dependency for your app.
var myApp = angular;
NOTE: The module name "door3.css" is now deprecated.
This module can be used by adding a css property in your routes values, directives or by calling the
$css service methods from controllers and services.
The css property supports a string, an array of strings, object notation or an array of objects.
See examples below for more information.
For Routes (Angular's ngRoute)
Requires ngRoute as a dependency
For States (UI Router)
Requires ui.router as a dependency
AngularCSS supports "smart media queries". This means that stylesheets with media queries will be only added when the breakpoint matches. This will significantly optimize the load time of your apps.
Even though you can use the
media property to specify media queries, the best way to manage your breakpoints is by settings them in the provider's defaults. For example:
You can configure AngularCSS at the global level or at the stylesheet level.
Configuring global options
These options are applied during the
config phase of your app via
Configuring CSS options
These options are applied at the stylesheet level.
css:href: 'file-path.css'rel: 'stylesheet'type: 'text/css'media: falsepersist: falsepreload: falsebustCache: falseweight: 0
AngularCSS is fully supported by AngularJS 1.3+ Now added AngularJS 1.5 support for .component()
There is partial support for AngularJS 1.2. It does not support
css property via DDO (Directive Definition Object).
The workaround is to bind (or add) the CSS in the directive's controller or link function via
Can I use AngularCSS in Angular 2?
AngularCSS is not necessary in Angular 2! Angular 2 ships with a similar feature out of the box. It's called 'styleUrls' and it looks like this:
Chrome, Firefox, Safari, iOS Safari, Android and IE9+
IE9 Does not support matchMedia API. This means that in IE9, stylesheets with media queries will be added without checking if the breakpoint matches.
Copyright and license
The MIT License Copyright (c) 2016 Alex Castillo Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.