diff options
| author | Max Martinsson | 2012-06-07 17:25:35 +0200 |
|---|---|---|
| committer | Misko Hevery | 2012-09-06 16:06:21 -0700 |
| commit | cebd015f78c5e21bd37d4bc055dbcdc21dac2ef2 (patch) | |
| tree | 59fd08d49dd0186deee8b95d7dafdfe0dbc8c372 /src/ng/directive/ngClass.js | |
| parent | fbdab513dd48f667ad857030cf4b3481ecdd9097 (diff) | |
| download | angular.js-cebd015f78c5e21bd37d4bc055dbcdc21dac2ef2.tar.bz2 | |
fix(ngClass): works with class interpolation
Closes #1016
Diffstat (limited to 'src/ng/directive/ngClass.js')
| -rw-r--r-- | src/ng/directive/ngClass.js | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/src/ng/directive/ngClass.js b/src/ng/directive/ngClass.js index e054d4c6..6ba73e60 100644 --- a/src/ng/directive/ngClass.js +++ b/src/ng/directive/ngClass.js @@ -3,17 +3,25 @@ function classDirective(name, selector) { name = 'ngClass' + name; return ngDirective(function(scope, element, attr) { - scope.$watch(attr[name], function(newVal, oldVal) { + // Reusable function for re-applying the ngClass + function reapply(newVal, oldVal) { if (selector === true || scope.$index % 2 === selector) { if (oldVal && (newVal !== oldVal)) { - if (isObject(oldVal) && !isArray(oldVal)) - oldVal = map(oldVal, function(v, k) { if (v) return k }); - element.removeClass(isArray(oldVal) ? oldVal.join(' ') : oldVal); - } - if (isObject(newVal) && !isArray(newVal)) - newVal = map(newVal, function(v, k) { if (v) return k }); - if (newVal) element.addClass(isArray(newVal) ? newVal.join(' ') : newVal); } - }, true); + if (isObject(oldVal) && !isArray(oldVal)) + oldVal = map(oldVal, function(v, k) { if (v) return k }); + element.removeClass(isArray(oldVal) ? oldVal.join(' ') : oldVal); + } + if (isObject(newVal) && !isArray(newVal)) + newVal = map(newVal, function(v, k) { if (v) return k }); + if (newVal) element.addClass(isArray(newVal) ? newVal.join(' ') : newVal); + } + }; + scope.$watch(attr[name], reapply, true); + + attr.$observe('class', function(value) { + var ngClass = scope.$eval(attr[name]); + reapply(ngClass, ngClass); + }); }); } |
