Skip to content

Commit 398e381

Browse files
committed
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. Fixes angular#1637
1 parent 7eafbb9 commit 398e381

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/ng/directive/ngClass.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ function classDirective(name, selector) {
2626
}
2727

2828

29-
function ngClassWatchAction(newVal, oldVal) {
29+
var oldVal = undefined;
30+
function ngClassWatchAction(newVal) {
3031
if (selector === true || scope.$index % 2 === selector) {
3132
if (oldVal && (newVal !== oldVal)) {
3233
removeClass(oldVal);
3334
}
3435
addClass(newVal);
3536
}
37+
oldVal = newVal;
3638
}
3739

3840

test/ng/directive/ngClassSpec.js

+9
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,15 @@ describe('ngClass', function() {
235235
expect(element.hasClass('too')).toBeFalsy();
236236
}));
237237

238+
it('should not mess up class application due to observing an interpolated class attribute', inject(function($rootScope, $compile) {
239+
$rootScope.foo = true;
240+
$rootScope.$watch("anything", function() {
241+
$rootScope.foo = false;
242+
});
243+
element = $compile('<div ng-class="{foo:foo}"/>')($rootScope);
244+
$rootScope.$digest();
245+
expect(element.hasClass('foo')).toBeFalsy();
246+
}));
238247

239248
it('should update ngClassOdd/Even when model is changed by filtering', inject(function($rootScope, $compile) {
240249
element = $compile('<ul>' +

0 commit comments

Comments
 (0)