Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 5f5d4fe

Browse files
provegardIgorMinar
authored andcommitted
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
1 parent 791804b commit 5f5d4fe

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/ng/directive/ngClass.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
function classDirective(name, selector) {
44
name = 'ngClass' + name;
55
return ngDirective(function(scope, element, attr) {
6+
var oldVal = undefined;
67

78
scope.$watch(attr[name], ngClassWatchAction, true);
89

@@ -26,13 +27,14 @@ function classDirective(name, selector) {
2627
}
2728

2829

29-
function ngClassWatchAction(newVal, oldVal) {
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

+11
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,17 @@ describe('ngClass', function() {
236236
}));
237237

238238

239+
it('should not mess up class value due to observing an interpolated class attribute', inject(function($rootScope, $compile) {
240+
$rootScope.foo = true;
241+
$rootScope.$watch("anything", function() {
242+
$rootScope.foo = false;
243+
});
244+
element = $compile('<div ng-class="{foo:foo}"></div>')($rootScope);
245+
$rootScope.$digest();
246+
expect(element.hasClass('foo')).toBe(false);
247+
}));
248+
249+
239250
it('should update ngClassOdd/Even when model is changed by filtering', inject(function($rootScope, $compile) {
240251
element = $compile('<ul>' +
241252
'<li ng-repeat="i in items" ' +

0 commit comments

Comments
 (0)