diff options
| author | Igor Minar | 2012-03-08 15:00:38 -0800 | 
|---|---|---|
| committer | Igor Minar | 2012-03-08 22:29:34 -0800 | 
| commit | f54db2ccda399f2677e4ca7588018cb31545a2b4 (patch) | |
| tree | 29ef2f8f834544c84cea1a82e3d08679358fb992 /src/directive/ngStyle.js | |
| parent | dd7b0f56fcd9785f7fccae8c4f088a8f3e7b125e (diff) | |
| download | angular.js-f54db2ccda399f2677e4ca7588018cb31545a2b4.tar.bz2 | |
chore(directives,widgets): reorg the code under directive/ dir
Diffstat (limited to 'src/directive/ngStyle.js')
| -rw-r--r-- | src/directive/ngStyle.js | 42 | 
1 files changed, 42 insertions, 0 deletions
diff --git a/src/directive/ngStyle.js b/src/directive/ngStyle.js new file mode 100644 index 00000000..cd2c1e3c --- /dev/null +++ b/src/directive/ngStyle.js @@ -0,0 +1,42 @@ +'use strict'; + +/** + * @ngdoc directive + * @name angular.module.ng.$compileProvider.directive.ng:style + * + * @description + * The ng:style allows you to set CSS style on an HTML element conditionally. + * + * @element ANY + * @param {expression} expression {@link guide/dev_guide.expressions Expression} which evals to an + *      object whose keys are CSS style names and values are corresponding values for those CSS + *      keys. + * + * @example +   <doc:example> +     <doc:source> +        <input type="button" value="set" ng:click="myStyle={color:'red'}"> +        <input type="button" value="clear" ng:click="myStyle={}"> +        <br/> +        <span ng:style="myStyle">Sample Text</span> +        <pre>myStyle={{myStyle}}</pre> +     </doc:source> +     <doc:scenario> +       it('should check ng:style', function() { +         expect(element('.doc-example-live span').css('color')).toBe('rgb(0, 0, 0)'); +         element('.doc-example-live :button[value=set]').click(); +         expect(element('.doc-example-live span').css('color')).toBe('rgb(255, 0, 0)'); +         element('.doc-example-live :button[value=clear]').click(); +         expect(element('.doc-example-live span').css('color')).toBe('rgb(0, 0, 0)'); +       }); +     </doc:scenario> +   </doc:example> + */ +var ngStyleDirective = ngDirective(function(scope, element, attr) { +  scope.$watch(attr.ngStyle, function(newStyles, oldStyles) { +    if (oldStyles && (newStyles !== oldStyles)) { +      forEach(oldStyles, function(val, style) { element.css(style, '');}); +    } +    if (newStyles) element.css(newStyles); +  }, true); +});  | 
