Skip to content

Commit 30516b7

Browse files
author
rgaskill
committed
fix(ngClass) when reversing the orderByFilter ngClassOdd and ngClassEven is not updated.
Issue angular#1563 fix. Put back in place the original fix issue angular#1076
1 parent 19a324c commit 30516b7

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed

src/ng/directive/ngClass.js

+32-7
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,46 @@ function classDirective(name, selector) {
77
function ngClassWatchAction(newVal, oldVal) {
88
if (selector === true || scope.$index % 2 === selector) {
99
if (oldVal && (newVal !== oldVal)) {
10-
if (isObject(oldVal) && !isArray(oldVal))
11-
oldVal = map(oldVal, function(v, k) { if (v) return k });
12-
element.removeClass(isArray(oldVal) ? oldVal.join(' ') : oldVal);
10+
removeClass(oldVal);
1311
}
14-
if (isObject(newVal) && !isArray(newVal))
15-
newVal = map(newVal, function(v, k) { if (v) return k });
16-
if (newVal) element.addClass(isArray(newVal) ? newVal.join(' ') : newVal);
12+
addClass(newVal);
1713
}
1814
};
19-
scope.$watch(attr[name], ngClassWatchAction, true);
15+
16+
function removeClass(classVal) {
17+
if (isObject(classVal) && !isArray(classVal))
18+
classVal = map(classVal, function(v, k) { if (v) return k });
19+
element.removeClass(isArray(classVal) ? classVal.join(' ') : classVal);
20+
}
21+
22+
function addClass(classVal) {
23+
if (isObject(classVal) && !isArray(classVal))
24+
classVal = map(classVal, function(v, k) { if (v) return k });
25+
if (classVal) element.addClass(isArray(classVal) ? classVal.join(' ') : classVal);
26+
}
27+
28+
scope.$watch(attr[name], ngClassWatchAction, true);
29+
30+
function indexWatch(newVal, oldVal){
31+
if (newVal !== oldVal && ((newVal + oldVal) % 2 === 1)) {
32+
if (newVal % 2 !== selector) {
33+
removeClass(scope.$eval(attr[name]));
34+
} else {
35+
addClass(scope.$eval(attr[name]));
36+
}
37+
}
38+
};
39+
40+
if(selector !== true) {
41+
indexWatch(scope.$index);
42+
scope.$watch("$index", indexWatch, true);
43+
}
2044

2145
attr.$observe('class', function(value) {
2246
var ngClass = scope.$eval(attr[name]);
2347
ngClassWatchAction(ngClass, ngClass);
2448
});
49+
2550
});
2651
}
2752

test/ng/directive/ngClassSpec.js

+27
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,31 @@ describe('ngClass', function() {
278278
expect(e2.hasClass('even')).toBeTruthy();
279279
expect(e2.hasClass('odd')).toBeFalsy();
280280
}));
281+
282+
283+
it('should ngClass odd/even with orderBy direction change', inject(function($rootScope, $compile) {
284+
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);
285+
$rootScope.values = [{val: 0},{val: 1}];
286+
$rootScope.sort = {column: 'val', desc: false};
287+
$rootScope.$digest();
288+
289+
var e1 = jqLite(element[0].childNodes[1]);
290+
var e2 = jqLite(element[0].childNodes[2]);
291+
expect(e1.text()).toBe('0');
292+
expect(e1.hasClass('existing')).toBeTruthy();
293+
expect(e1.hasClass('odd')).toBeTruthy();
294+
expect(e2.hasClass('existing')).toBeTruthy();
295+
expect(e2.hasClass('even')).toBeTruthy();
296+
297+
$rootScope.sort.desc = true;
298+
$rootScope.$digest();
299+
300+
e1 = jqLite(element[0].childNodes[1]);
301+
e2 = jqLite(element[0].childNodes[2]);
302+
expect(e1.text()).toBe('1');
303+
expect(e1.hasClass('existing')).toBeTruthy();
304+
expect(e1.hasClass('odd')).toBeTruthy();
305+
expect(e2.hasClass('existing')).toBeTruthy();
306+
expect(e2.hasClass('even')).toBeTruthy();
307+
}));
281308
});

0 commit comments

Comments
 (0)