From 79bb7b1f0b6ded0ca84660f387f406de98f5bdf1 Mon Sep 17 00:00:00 2001 From: Max Martinsson Date: Thu, 7 Jun 2012 17:25:35 +0200 Subject: fix(ngClass): works with class interpolation Closes #1016 --- src/ng/directive/ngClass.js | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'src/ng') 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); + }); }); } -- cgit v1.2.3