| OLD | NEW |
| 1 part of angular.directive; | 1 part of angular.directive; |
| 2 | 2 |
| 3 /** | 3 /** |
| 4 * The `ngStyle` directive allows you to set CSS style on an HTML element | 4 * The `ngStyle` directive allows you to set CSS style on an HTML element condi
tionally. |
| 5 * conditionally. | |
| 6 * | 5 * |
| 7 * @example | 6 * @example |
| 8 * <span ng-style="{color:'red'}">Sample Text</span> | 7 <span ng-style="{color:'red'}">Sample Text</span> |
| 9 */ | 8 */ |
| 10 @NgDirective( | 9 @NgDirective( |
| 11 selector: '[ng-style]', | 10 selector: '[ng-style]', |
| 12 map: const { 'ng-style': '@styleExpression'}) | 11 map: const { 'ng-style': '@styleExpression'}) |
| 13 class NgStyleDirective { | 12 class NgStyleDirective { |
| 14 final dom.Element _element; | 13 dom.Element _element; |
| 15 final Scope _scope; | 14 Scope _scope; |
| 16 final AstParser _parser; | |
| 17 | 15 |
| 18 String _styleExpression; | 16 String _styleExpression; |
| 19 Watch _watch; | |
| 20 | 17 |
| 21 NgStyleDirective(this._element, this._scope, this._parser); | 18 NgStyleDirective(this._element, this._scope); |
| 19 |
| 20 Function _removeWatch = () => null; |
| 21 var _lastStyles; |
| 22 | 22 |
| 23 /** | 23 /** |
| 24 * ng-style attribute takes an expression which evaluates to an | 24 * ng-style attribute takes an expression which evaluates to an |
| 25 * object whose keys are CSS style names and values are corresponding values | 25 * object whose keys are CSS style names and values are corresponding valu
es for those CSS |
| 26 * for those CSS keys. | 26 * keys. |
| 27 */ | 27 */ |
| 28 set styleExpression(String value) { | 28 set styleExpression(String value) { |
| 29 _styleExpression = value; | 29 _styleExpression = value; |
| 30 if (_watch != null) _watch.remove(); | 30 _removeWatch(); |
| 31 _watch = _scope.watch(_parser(_styleExpression, collection: true), _onStyleC
hange); | 31 _removeWatch = _scope.$watchCollection(_styleExpression, _onStyleChange); |
| 32 } | 32 } |
| 33 | 33 |
| 34 _onStyleChange(MapChangeRecord mapChangeRecord, _) { | 34 _onStyleChange(Map newStyles) { |
| 35 if (mapChangeRecord != null) { | 35 dom.CssStyleDeclaration css = _element.style; |
| 36 dom.CssStyleDeclaration css = _element.style; | 36 if (_lastStyles != null) { |
| 37 fn(MapKeyValue kv) => css.setProperty(kv.key, kv.currentValue == null ? ''
: kv.currentValue); | 37 _lastStyles.forEach((val, style) { css.setProperty(val, ''); }); |
| 38 } |
| 39 _lastStyles = newStyles; |
| 38 | 40 |
| 39 mapChangeRecord | 41 if (newStyles != null) { |
| 40 ..forEachRemoval(fn) | 42 newStyles.forEach((val, style) { css.setProperty(val, style); }); |
| 41 ..forEachChange(fn) | |
| 42 ..forEachAddition(fn); | |
| 43 } | 43 } |
| 44 } | 44 } |
| 45 } | 45 } |
| OLD | NEW |