diff options
| author | Max Martinsson | 2012-06-07 17:25:35 +0200 |
|---|---|---|
| committer | Misko Hevery | 2012-09-06 15:49:48 -0700 |
| commit | 79bb7b1f0b6ded0ca84660f387f406de98f5bdf1 (patch) | |
| tree | 6f885f3f225b5145fd4c4f4281df8584c6a60c36 | |
| parent | b936e52874fe0173c6d4ba0a84f45deac67518ac (diff) | |
| download | angular.js-79bb7b1f0b6ded0ca84660f387f406de98f5bdf1.tar.bz2 | |
fix(ngClass): works with class interpolation
Closes #1016
| -rw-r--r-- | src/ng/directive/ngClass.js | 26 | ||||
| -rw-r--r-- | test/ng/directive/ngClassSpec.js | 33 |
2 files changed, 50 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); + }); }); } diff --git a/test/ng/directive/ngClassSpec.js b/test/ng/directive/ngClassSpec.js index e8685b7d..fc46bf7c 100644 --- a/test/ng/directive/ngClassSpec.js +++ b/test/ng/directive/ngClassSpec.js @@ -201,4 +201,37 @@ describe('ngClass', function() { expect(e2.hasClass('C')).toBeFalsy(); expect(e2.hasClass('D')).toBeFalsy(); })); + + + it('should reapply ngClass when interpolated class attribute changes', inject(function($rootScope, $compile) { + element = $compile('<div class="one {{cls}} three" ng-class="{four: four}"></div>')($rootScope); + + $rootScope.$apply(function() { + $rootScope.cls = "two"; + $rootScope.four = true; + }); + expect(element).toHaveClass('one'); + expect(element).toHaveClass('two'); // interpolated + expect(element).toHaveClass('three'); + expect(element).toHaveClass('four'); + + $rootScope.$apply(function() { + $rootScope.cls = "too"; + }); + expect(element).toHaveClass('one'); + expect(element).toHaveClass('too'); // interpolated + expect(element).toHaveClass('three'); + expect(element).toHaveClass('four'); // should still be there + expect(element.hasClass('two')).toBeFalsy(); + + $rootScope.$apply(function() { + $rootScope.cls = "to"; + }); + expect(element).toHaveClass('one'); + expect(element).toHaveClass('to'); // interpolated + expect(element).toHaveClass('three'); + expect(element).toHaveClass('four'); // should still be there + expect(element.hasClass('two')).toBeFalsy(); + expect(element.hasClass('too')).toBeFalsy(); + })); }); |
