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

fix(ngClass) when reversing the orderByFilter ngClassOdd and ngClassEven is not updated. #1564

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
39 changes: 32 additions & 7 deletions src/ng/directive/ngClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,46 @@ function classDirective(name, selector) {
function ngClassWatchAction(newVal, oldVal) {
if (selector === true || scope.$index % 2 === selector) {
if (oldVal && (newVal !== oldVal)) {
if (isObject(oldVal) && !isArray(oldVal))
oldVal = map(oldVal, function(v, k) { if (v) return k });
element.removeClass(isArray(oldVal) ? oldVal.join(' ') : oldVal);
removeClass(oldVal);
}
if (isObject(newVal) && !isArray(newVal))
newVal = map(newVal, function(v, k) { if (v) return k });
if (newVal) element.addClass(isArray(newVal) ? newVal.join(' ') : newVal);
addClass(newVal);
}
};
scope.$watch(attr[name], ngClassWatchAction, true);

function removeClass(classVal) {
if (isObject(classVal) && !isArray(classVal))
classVal = map(classVal, function(v, k) { if (v) return k });
element.removeClass(isArray(classVal) ? classVal.join(' ') : classVal);
}

function addClass(classVal) {
if (isObject(classVal) && !isArray(classVal))
classVal = map(classVal, function(v, k) { if (v) return k });
if (classVal) element.addClass(isArray(classVal) ? classVal.join(' ') : classVal);
}

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

function indexWatch(newVal, oldVal){
if (newVal !== oldVal && ((newVal + oldVal) % 2 === 1)) {
if (newVal % 2 !== selector) {
removeClass(scope.$eval(attr[name]));
} else {
addClass(scope.$eval(attr[name]));
}
}
};

if(selector !== true) {
indexWatch(scope.$index);
scope.$watch("$index", indexWatch, true);
}

attr.$observe('class', function(value) {
var ngClass = scope.$eval(attr[name]);
ngClassWatchAction(ngClass, ngClass);
});

});
}

Expand Down
27 changes: 27 additions & 0 deletions test/ng/directive/ngClassSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,31 @@ describe('ngClass', function() {
expect(e2.hasClass('even')).toBeTruthy();
expect(e2.hasClass('odd')).toBeFalsy();
}));


it('should ngClass odd/even with orderBy direction change', inject(function($rootScope, $compile) {
element = $compile('<ul><li ng-repeat="i in values | orderBy:sort.column:sort.desc" class="existing" ng-class-odd="\'odd\'" ng-class-even="\'even\'">{{i.val}}</li><ul>')($rootScope);
$rootScope.values = [{val: 0},{val: 1}];
$rootScope.sort = {column: 'val', desc: false};
$rootScope.$digest();

var e1 = jqLite(element[0].childNodes[1]);
var e2 = jqLite(element[0].childNodes[2]);
expect(e1.text()).toBe('0');
expect(e1.hasClass('existing')).toBeTruthy();
expect(e1.hasClass('odd')).toBeTruthy();
expect(e2.hasClass('existing')).toBeTruthy();
expect(e2.hasClass('even')).toBeTruthy();

$rootScope.sort.desc = true;
$rootScope.$digest();

e1 = jqLite(element[0].childNodes[1]);
e2 = jqLite(element[0].childNodes[2]);
expect(e1.text()).toBe('1');
expect(e1.hasClass('existing')).toBeTruthy();
expect(e1.hasClass('odd')).toBeTruthy();
expect(e2.hasClass('existing')).toBeTruthy();
expect(e2.hasClass('even')).toBeTruthy();
}));
});