From 52a263add5741443f2958aec083811cec72dfa92 Mon Sep 17 00:00:00 2001 From: petrovalex Date: Tue, 14 Aug 2012 20:38:05 +0300 Subject: [PATCH 1/2] fix(ngClassOdd/Even): proper update with filter --- src/ng/directive/ngClass.js | 35 ++++++++++++++++++++----- test/ng/directive/ngClassSpec.js | 44 ++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 7 deletions(-) diff --git a/src/ng/directive/ngClass.js b/src/ng/directive/ngClass.js index e054d4c6d7a4..9b0f93bfde8c 100644 --- a/src/ng/directive/ngClass.js +++ b/src/ng/directive/ngClass.js @@ -3,17 +3,38 @@ function classDirective(name, selector) { name = 'ngClass' + name; return ngDirective(function(scope, element, attr) { + if (selector !== true) { + scope.$watch("$index", function(newVal, oldVal) { + if (newVal !== oldVal && ((newVal + oldVal) % 2 === 1)) { + if (newVal % 2 !== selector) { + removeClass(scope.$eval(attr[name])); + } else { + addClass(scope.$eval(attr[name])); + } + } + }, true); + } + scope.$watch(attr[name], function(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); - } - if (isObject(newVal) && !isArray(newVal)) - newVal = map(newVal, function(v, k) { if (v) return k }); - if (newVal) element.addClass(isArray(newVal) ? newVal.join(' ') : newVal); } + removeClass(oldVal); + } + addClass(newVal); + } }, 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); + } }); } diff --git a/test/ng/directive/ngClassSpec.js b/test/ng/directive/ngClassSpec.js index e8685b7dc0bb..7604f3820737 100644 --- a/test/ng/directive/ngClassSpec.js +++ b/test/ng/directive/ngClassSpec.js @@ -201,4 +201,48 @@ describe('ngClass', function() { expect(e2.hasClass('C')).toBeFalsy(); expect(e2.hasClass('D')).toBeFalsy(); })); + + + it('should update ngClassOdd/Even when model is changed by filtering', inject(function($rootScope, $compile) { + element = $compile('