Download From GitHub

ngCss

Angular Binding in CSS

Overview

ngCss gives you the ability to embed
<script>
tags within CSS - adding variables, functions and logic to the power of CSS. Our (twisted) sister project: CjsSS.js Check out the Vanilla Javascript (twisted) sister project CjsSS.js for Javascript binding in CSS sans Angular.

Options

Option values can be passed into ngCss in a number of locations:
  1. <div style="color: color;" ng-css="{ scope: '#myCss', live: true }">...</div>

    overrides:
  2. <style type="text/css" id="myCss" ng-css="{ live: false }" />...</style>

    or
    <link rel="stylesheet" type="text/css" id="myCss" href="css/site.css" ng-css="{ live: false }" />

    overrides:
  3. When placed on the first line within the CSS,
    /*ngcss({ live: true })*/

    overrides:
  4. The default values described below (which can be programmatically overridden).
In the example above,
options.live
will be set to
true
within the context of the
DIV
as the
DIV
tag's option values override the
STYLE
/
LINK
tags' option values.
options.live
will be set to
false
within the context of the
STYLE
/
LINK
tags' which overrides the CSS's inline option values.
options.live
was set to
true
within the CSS's inline option values which would have overriden the default value (
false
), but it never takes effect due to the
STYLE
/
LINK
tags' option values.
The use of the CSS's inline option values (#3) allows you to fully configure all aspects of ngCss within the CSS itself.

script

VARIANT (see valid values below) "isolated" The Javascript scope that the CSS embedded
SCRIPT
tags will be
eval
uated within:

callback

FUNCTION undefined Defines the function** that will be called each time the CSS is updated. You can disable the callback by returning
false
from the function.

scope

VARIANT (see valid values below) "" (e.g. a
null-string
)
The Angular
$scope
that will be used by the directive:

live

BOOLEAN false Specifies if changes made within the
$scope
are to be automatically reflected within the CSS. When set to
false
, the custom
$broadcast
event updateCss is configured (e.g.
$rootScope.$broadcast('updateCss');
) and can be triggered via the factory's
ngCss.updateCss()
or via
$rootScope.$broadcast('updateCss');
.

async

BOOLEAN true Specifies if
LINK
tags are to be processed asynchronously. If ordering matters in the execution of the embedded
SCRIPT
s, then this should be set to
false
.
† - There is no reason to use
sandbox
over
isolated
if your CSS embedded
SCRIPT
tags are from a trusted source, especially considering the limitations of postMessage.
‡ - Allows for the sharing of functionality and variables across invocations.
* -
object
.script
must create a new Javascript scope; it cannot link to an existing scope.
** -
function
s are passed 2 arguments:
  1. domElement
    - the directive's DOM element.
  2. metadata
    - an object representing the internal metadata pertaining to the element.
Each
ng-css
attributed
LINK
tag is replaced with a
STYLE
tag with only the
ng-css
attribute copied across. If you require additional attributes or processing to apply to these created
STYLE
tags, you can make any necessary modifications to the DOM element within a
function
.
Options settings can reference properties in the context of the original Angular scope, but not within the Angular scope created/imported via
options.scope
.

style
Attributes

Variable
s can be used within any tag's
style
attribute (but
SCRIPT
tags are not permitted). When applying ngCss to a
style
attribute, you have two options when setting the value of
ng-css
's in-line options: Only the
style
attribute is effected by ngCss in this context using a non-isolate
$scope
(even when the referenced element is!). This is accomplished by a bit of pre- and post-
$compile
slight-of-hand between the directive's
template
and
link
functions and custom linking code via a
$watch
(
live: true
) or
$on
(
live: false
).
Due to IE's behavior of removing invalid CSS settings from
STYLE
attributes at parse (which in turn, removes them from the DOM) it is necessary to specify ngCss styles in a
ng-css-style
(or HTML5-compliant
data-ng-css-style
) attribute. The
ng-css-style
(or HTML5-compliant
data-ng-css-style
) attribute is concatnated with the additionally-defined
STYLE
attribute if it is present.

How To Override The Default Option Values

If you wish to override any of the default option values you can call
ngCss.defaults
, passing in the new default option values like so:
                var myAngularApp = angular.module("myAngularApp", ["ngCss"]);
                
                myAngularApp.controller("ExampleController", ["$scope", "ngCss", function ($scope, ngCss) {
                    ngCss.defaults({ script: 'global' });
                }]);
                
In the example above, we reset the default value for
options.script
within ngCss to
global
.

Getting Started

Assuming the default option values are in effect as described above:
  1. Download ngCss From GitHub
  2. Include the ngCss
    SCRIPT
    tag in the
    HEAD
    of your HTML document below your Angular
    SCRIPT
    tag:
                            <script type="text/javascript" src="angular.min.js"></script>
                            <script type="text/javascript" src="angular.ngcss.min.js"></script>
                            
  3. Include the attribute
    ng-app="myAngularApp"
    in your HTML element (e.g.
    <html ng-app="myAngularApp">
    ) and setup
    myAngularApp
    to include the ngCss factory like so:
                            <script>
                                var myAngularApp = angular.module("myAngularApp", ["ngCss"]);
                            </script>
                            
  4. Add the
    ng-css
    attribute to the LINK and/or STYLE elements we want to process:
                            <style id="myCss" type="text/css" ng-css>...</style>
                            
                            <link id="myExCss" rel="stylesheet" type="text/css" href="css/site.css" ng-css/>
                            
  5. Define the
    options
    used by ngCss (if any):
                            <style id="myCss" type="text/css" ng-css="{ script: 'local', live: true }">...</style>
                            
                            <link id="myExCss" rel="stylesheet" type="text/css" href="css/site.css"
                                ng-css="{ script: 'local', live: true }"
                            />
                            
  6. Edit the CSS within the LINK and/or STYLE elements to use ngCss like this:
                            /*
                            <script>
                                $scope.color = 'blue';
                            </script>
                            */
                            DIV {
                                color: /*color*/;
                            }
                            
  7. (Optional) Consume ngCss (or any other
    $scope
    's)
    Variable
    s within any element's
    style
    attribute like this:
                            <span style="color: color;" ng-css="#myCss">...</span>
                        
                            <a href="http://opensourcetaekwondo.com/ngcss" style="color: color;"
                                ng-css="{ scope: '#myExCss', live: false }"
                            >...</span>
                            
    Technically, you can import any Angular element's
    $scope
    into the
    style
    attribute, but the intention is for you to reference
    ng-css
    attributed
    LINK
    or
    STYLE
    elements.
NOTE: You are also permitted to use the attribute name
data-ng-css
in place of
ng-css
if you want to be HTML5 compliant.

Examples

Configure your own example of ngCss:
❮ Choose Your Options
Reports
$scope.$id
's

Complex Example

This page is setup as a complex example; be sure to view source and explore the code for more examples and ideas. The two live-bound STYLE elements share one
$scope
and are setup to allow for in-line SCRIPTs (so all of the data is setup within the STYLE element itself, not in the
ng-controller
) and it imports the
$scope
from the
ng-controller
. Have a play with the various CSS values and see the changes live in this page:
Selector Property Value
DIV.gotchas background-color
BODY color
BODY font-family
[Pretty Font] font-family
[Main Color] background-color

Gotchas

Side Effects

What is up with the 3-headed knight?

ngCss (Angular Cascading Style Sheet) is the union of three web technologies joined seemlessly (unnaturally?) into one solution. Like CjsSS.js, the union of multiple technologies has been logo-ized as a multi-headed beastie and since Angular's logo is a shield we look to...

The Tale Of Sir Robin (which is not to be confused with The Knights Who Say... NI!).

ngCss is an Angular Directive+Filter+Factory made by Nick Campbell, © 2014-2015.

ngCss is released under the MIT License. See the license.txt file in the source code for copy permission.

Nick is a web nerd who likes to tinker in his spare time; ngCss is a product of that tinkering. Development is not supported by advertising nor the marketing of your email addresses. If you find this code useful, please consider tossing a few dollars into my tip jar. If you are a company that finds this code useful, please feel free to toss more than a few dollars into my tip jar ;)