diff --git a/src/ng/directive/ngClass.js b/src/ng/directive/ngClass.js index 13b0378a9f13..e19d433b493f 100644 --- a/src/ng/directive/ngClass.js +++ b/src/ng/directive/ngClass.js @@ -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); }); + }); } diff --git a/test/ng/directive/ngClassSpec.js b/test/ng/directive/ngClassSpec.js index 4a53030bd21f..dbc1f8d40314 100644 --- a/test/ng/directive/ngClassSpec.js +++ b/test/ng/directive/ngClassSpec.js @@ -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('