diff options
| author | Per RovegÄrd | 2013-02-16 00:32:38 +0100 |
|---|---|---|
| committer | Igor Minar | 2013-02-18 20:25:43 -0800 |
| commit | 5f5d4feadbfa9d8ecc8150041dfd2bca2b2e9fea (patch) | |
| tree | 33aeab565f5d585f41c26bd95fa957f88d797ca7 | |
| parent | 791804bdbfa6da7a39283623bd05628a01cd8720 (diff) | |
| download | angular.js-5f5d4feadbfa9d8ecc8150041dfd2bca2b2e9fea.tar.bz2 | |
fix(ngClass): keep track of old ngClass value manually
ngClassWatchAction, when called as a $watch function, gets the wrong old
value after it has been invoked previously due to observation of the
interpolated class attribute. As a result it doesn't remove classes
properly. Keeping track of the old value manually seems to fix this.
Closes #1637
| -rw-r--r-- | src/ng/directive/ngClass.js | 4 | ||||
| -rw-r--r-- | test/ng/directive/ngClassSpec.js | 11 |
2 files changed, 14 insertions, 1 deletions
diff --git a/src/ng/directive/ngClass.js b/src/ng/directive/ngClass.js index 79c55d7a..d731118f 100644 --- a/src/ng/directive/ngClass.js +++ b/src/ng/directive/ngClass.js @@ -3,6 +3,7 @@ function classDirective(name, selector) { name = 'ngClass' + name; return ngDirective(function(scope, element, attr) { + var oldVal = undefined; scope.$watch(attr[name], ngClassWatchAction, true); @@ -26,13 +27,14 @@ function classDirective(name, selector) { } - function ngClassWatchAction(newVal, oldVal) { + function ngClassWatchAction(newVal) { if (selector === true || scope.$index % 2 === selector) { if (oldVal && (newVal !== oldVal)) { removeClass(oldVal); } addClass(newVal); } + oldVal = newVal; } diff --git a/test/ng/directive/ngClassSpec.js b/test/ng/directive/ngClassSpec.js index 4a53030b..69afef7a 100644 --- a/test/ng/directive/ngClassSpec.js +++ b/test/ng/directive/ngClassSpec.js @@ -236,6 +236,17 @@ describe('ngClass', function() { })); + it('should not mess up class value due to observing an interpolated class attribute', inject(function($rootScope, $compile) { + $rootScope.foo = true; + $rootScope.$watch("anything", function() { + $rootScope.foo = false; + }); + element = $compile('<div ng-class="{foo:foo}"></div>')($rootScope); + $rootScope.$digest(); + expect(element.hasClass('foo')).toBe(false); + })); + + it('should update ngClassOdd/Even when model is changed by filtering', inject(function($rootScope, $compile) { element = $compile('<ul>' + '<li ng-repeat="i in items" ' + |
