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

Commit

Permalink
fix(ngClass): keep track of old ngClass value manually
Browse files Browse the repository at this point in the history
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
  • Loading branch information
provegard authored and IgorMinar committed Feb 19, 2013
1 parent 791804b commit 5f5d4fe
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/ng/directive/ngClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
}


Expand Down
11 changes: 11 additions & 0 deletions test/ng/directive/ngClassSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" ' +
Expand Down

0 comments on commit 5f5d4fe

Please sign in to comment.