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

fix(ngClass): handle index change correctly #7262

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
5 changes: 2 additions & 3 deletions src/ng/directive/ngClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ function classDirective(name, selector) {

if (name !== 'ngClass') {
scope.$watch('$index', function($index, old$index) {
// jshint bitwise: false
var mod = $index & 1;
if (mod !== old$index & 1) {
var mod = $index % 2;
if (mod !== old$index % 2) {
var classes = arrayClasses(scope.$eval(attr[name]));
mod === selector ?
addClasses(classes) :
Expand Down
21 changes: 21 additions & 0 deletions test/ng/directive/ngClassSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,27 @@ describe('ngClass', function() {
}));


it('should update ngClassOdd/Even when model is changed by adding', inject(function($rootScope, $compile) {
element = $compile('<ul>' +
'<li ng-repeat="i in items" ' +
'ng-class-odd="\'odd\'" ng-class-even="\'even\'">i</li>' +
'<ul>')($rootScope);
$rootScope.items = ['b','c','d'];
$rootScope.$digest();

$rootScope.items.unshift('a');
$rootScope.$digest();

var e1 = jqLite(element[0].childNodes[1]);
var e4 = jqLite(element[0].childNodes[7]);

expect(e1.hasClass('odd')).toBeTruthy();
expect(e1.hasClass('even')).toBeFalsy();

expect(e4.hasClass('even')).toBeTruthy();
expect(e4.hasClass('odd')).toBeFalsy();
}));

it('should update ngClassOdd/Even when model is changed by filtering', inject(function($rootScope, $compile) {
element = $compile('<ul>' +
'<li ng-repeat="i in items track by $index" ' +
Expand Down