From 162893fcc75aff0d52ccd784e46df2440147952a Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Fri, 15 Feb 2013 20:56:39 +0000 Subject: [PATCH 1/2] test(ngClass): should initialize correctly when $observe and $watch have different initial values This bug caused problems where ngClass was being used to identify whether a validation directive (such as required) was invalid, where the ng-model was being initialized to a valid value before the first digest completed. See this fiddle: http://jsfiddle.net/RobCannon/2AJCB/ --- test/ng/directive/ngClassSpec.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/test/ng/directive/ngClassSpec.js b/test/ng/directive/ngClassSpec.js index 4a53030bd21f..3dd4d0e27d62 100644 --- a/test/ng/directive/ngClassSpec.js +++ b/test/ng/directive/ngClassSpec.js @@ -52,7 +52,7 @@ describe('ngClass', function() { expect(element.hasClass('B')).toBeFalsy(); expect(element.hasClass('AnotB')).toBeTruthy(); - $rootScope.conditionB = function() { return true }; + $rootScope.conditionB = function() { return true; }; $rootScope.$digest(); expect(element.hasClass('existing')).toBeTruthy(); expect(element.hasClass('A')).toBeTruthy(); @@ -278,4 +278,15 @@ describe('ngClass', function() { expect(e2.hasClass('even')).toBeTruthy(); expect(e2.hasClass('odd')).toBeFalsy(); })); + + it('should initialize classes correctly when $observe and $watch provide different initial values', + inject(function($rootScope, $compile) { + $rootScope.x = 'test'; + element = $compile( + '
' + + '' + + '
')($rootScope); + $rootScope.$digest(); + expect(element.find('input').hasClass('error')).toBeFalsy(); + })); }); From 845c5922c8f585556e865f43f258836fffd0d2d2 Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Fri, 15 Feb 2013 20:56:57 +0000 Subject: [PATCH 2/2] fix(ngClass): should initialize correctly when $observe and $watch have different initial values This bug caused problems where ngClass was being used to identify whether a validation directive (such as required) was invalid, where the ng-model was being initialized to a valid value before the first digest completed. See this fiddle: http://jsfiddle.net/RobCannon/2AJCB/ --- src/ng/directive/ngClass.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ng/directive/ngClass.js b/src/ng/directive/ngClass.js index 79c55d7abdd6..70776ae3f303 100644 --- a/src/ng/directive/ngClass.js +++ b/src/ng/directive/ngClass.js @@ -3,12 +3,13 @@ function classDirective(name, selector) { name = 'ngClass' + name; return ngDirective(function(scope, element, attr) { + var oldVal; scope.$watch(attr[name], ngClassWatchAction, true); attr.$observe('class', function(value) { var ngClass = scope.$eval(attr[name]); - ngClassWatchAction(ngClass, ngClass); + ngClassWatchAction(ngClass); }); @@ -26,13 +27,14 @@ function classDirective(name, selector) { } - function ngClassWatchAction(newVal, oldVal) { + function ngClassWatchAction(newVal) { if (selector === true || scope.$index % 2 === selector) { if (oldVal && (newVal !== oldVal)) { removeClass(oldVal); } addClass(newVal); } + oldVal = newVal; }