<script>tags within CSS - adding variables, functions and logic to the power of CSS.
angular.ngcss.min.js
$scopeand
optionscan be initilized within the CSS itself, allowing for complete isolation of CSS-related code
objects via the css Filter
Variables within
styleattributes
$scopecan be isolated or imported from any Angular
$element
camelCaseto
dash-caseof Javascript
objectkeys converted auto-magicially
evaluated Javascript fully isolated by default
$broadcastevent updateCss
SCRIPTreferences within the CSS (e.g.
<script src='https://github.com/gka/chroma.js'></script>).
SCRIPTtag in the
HEADof your HTML document below your Angular
SCRIPTtag:
<script type="text/javascript" src="angular.min.js"></script> <script type="text/javascript" src="angular.ngcss.min.js"></script>
ng-app="myAngularApp"in your HTML element (e.g.
<html ng-app="myAngularApp">) and setup
myAngularAppto include the ngCss factory like so:
<script> var myAngularApp = angular.module("myAngularApp", ["ngCss"]); </script>
ng-cssattribute 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/>
optionsused 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 }" />
/* <script> $scope.color = 'blue'; </script> */ DIV { color: /*color*/; }
$scope's)
Variables within any element's
styleattribute like this:
Technically, you can import any Angular element's<span style="color: color;" ng-css="#myCss">...</span> <a href="http://opensourcetaekwondo.com/ngcss" style="color: color;" ng-css="{ scope: '#myExCss', live: false }" >...</span>
$scopeinto the
styleattribute, but the intention is for you to reference
ng-cssattributed
LINKor
STYLEelements.
data-ng-cssin place of
ng-cssif you want to be HTML5 compliant.
❮ Choose Your Options
|
||
Reports $scope.$id's |
||
$scopeand 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
$scopefrom 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 |
Variables (e.g.
/*Variable*/) as the braces used by Angular are also used within CSS and these additional non-CSS-valid braces can cause issues with CSS editors. But... this will require you to modify your ngCss-enabled *.css files before CSS minimization as the commented
Variables will likely be removed during minimization.
{{followed by
}}within your CSS will be eaten by
$interpolateor will otherwise cause errors. Make sure you have whitespace between all braces that are not Variables.
LINKtag CSS files are read via AJAX, the limitations of
XMLHttpRequestcan restrict where you serve your CSS files from. If your CSS files exist on the same server, this is a non-issue.
LINKtag is replaced by a new
STYLEtag that contains the modified CSS file contents. This process can break some relative URLs within your CSS files (e.g.
background-image: url("../img/a.png");). It is therefore recommended that you use absolute URLs within your CSS files (e.g.
background-image: url("/img/a.png");) in order to avoid any such errors.
SCRIPTs are all evaluated in series at initialization prior to CSS modification, so the following will not work as expected:
At initialization/*<script> $scope.color = 'blue'; </script>*/ DIV { color: /*color*/; //# expecting 'blue' but gets 'green' } /*<script> $scope.color = 'green'; </script>*/ SPAN { color: /*color*/; //# expecting 'green' }
$scope.colorwill be set to "blue" then "green" prior to any CSS modification, so the
colorof both DIV and SPAN will be set to "green".
LINKtag is replaced by a new
STYLEtag that contains the modified CSS file contents. This is done so that the original unprocessed CSS referenced by the
LINKtag does not apply to the webpage.
scriptvalue other than
sandbox, ngCss-embedded Javascript has the ability to modify the global scope.
$scope.
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 ;)