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

fix(ngClass): keep track of old ngClass value manually #2020

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/ng/directive/ngClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ function classDirective(name, selector) {
}


function ngClassWatchAction(newVal, oldVal) {
var oldVal = undefined;
function ngClassWatchAction(newVal) {
if (selector === true || scope.$index % 2 === selector) {
if (oldVal && (newVal !== oldVal)) {
removeClass(oldVal);
}
addClass(newVal);
}
oldVal = newVal;
}


Expand Down
9 changes: 9 additions & 0 deletions test/ng/directive/ngClassSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,15 @@ describe('ngClass', function() {
expect(element.hasClass('too')).toBeFalsy();
}));

it('should not mess up class application 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}"/>')($rootScope);
$rootScope.$digest();
expect(element.hasClass('foo')).toBeFalsy();
}));

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