Skip to content

Commit 07eeb04

Browse files
shahataRichardLitt
authored andcommitted
fix(ngClass): handle index changes when an item is unshifted
Closes angular#7256
1 parent f17f079 commit 07eeb04

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/ng/directive/ngClass.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function classDirective(name, selector) {
1919
scope.$watch('$index', function($index, old$index) {
2020
// jshint bitwise: false
2121
var mod = $index & 1;
22-
if (mod !== old$index & 1) {
22+
if (mod !== (old$index & 1)) {
2323
var classes = arrayClasses(scope.$eval(attr[name]));
2424
mod === selector ?
2525
addClasses(classes) :

test/ng/directive/ngClassSpec.js

+22
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,28 @@ describe('ngClass', function() {
278278
}));
279279

280280

281+
it('should update ngClassOdd/Even when an item is added to the model', inject(function($rootScope, $compile) {
282+
element = $compile('<ul>' +
283+
'<li ng-repeat="i in items" ' +
284+
'ng-class-odd="\'odd\'" ng-class-even="\'even\'">i</li>' +
285+
'<ul>')($rootScope);
286+
$rootScope.items = ['b','c','d'];
287+
$rootScope.$digest();
288+
289+
$rootScope.items.unshift('a');
290+
$rootScope.$digest();
291+
292+
var e1 = jqLite(element[0].childNodes[1]);
293+
var e4 = jqLite(element[0].childNodes[7]);
294+
295+
expect(e1.hasClass('odd')).toBeTruthy();
296+
expect(e1.hasClass('even')).toBeFalsy();
297+
298+
expect(e4.hasClass('even')).toBeTruthy();
299+
expect(e4.hasClass('odd')).toBeFalsy();
300+
}));
301+
302+
281303
it('should update ngClassOdd/Even when model is changed by filtering', inject(function($rootScope, $compile) {
282304
element = $compile('<ul>' +
283305
'<li ng-repeat="i in items track by $index" ' +

0 commit comments

Comments
 (0)