From 29f9e2665d8b771a6226870fc8fd2c4c94d7a2c0 Mon Sep 17 00:00:00 2001 From: Codier Date: Tue, 15 Nov 2011 18:45:36 -0600 Subject: fix(scope): $watch (and angular.equals) should support NaN values - since NaN !== NaN in javascript digest can get into an infinite loop when model value is set to NaN - angular.equals(NaN, NaN) should return true since that's what we expect when comparing primitives or objects containing NaN values Previously NaN because of its special === properties was used as the initial value for watches, but that results in issues when NaN is used as model value. In order to allow for model to be anything incuding undefined and NaN we need to mark the initial value differently in a way that would avoid these issues, allow us to run digest without major perf penalties and allow for clients to determine if the listener is being called because the watcher is being initialized or because the model changed. This implementation covers all of these scenarios. BREAKING CHANGE: previously to detect if the listener was called because the watcher was being initialized, it was suggested that clients check if old value is NaN. With this change, the check should be if the newVal equals the oldVal. Closes #657 --- src/directives.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/directives.js') diff --git a/src/directives.js b/src/directives.js index da540df5..41fbac16 100644 --- a/src/directives.js +++ b/src/directives.js @@ -576,7 +576,9 @@ function ngClass(selector) { return function(element) { this.$watch(expression, function(scope, newVal, oldVal) { if (selector(scope.$index)) { - if (oldVal) element.removeClass(isArray(oldVal) ? oldVal.join(' ') : oldVal); + if (oldVal && (newVal !== oldVal)) { + element.removeClass(isArray(oldVal) ? oldVal.join(' ') : oldVal); + } if (newVal) element.addClass(isArray(newVal) ? newVal.join(' ') : newVal); } }); @@ -823,7 +825,9 @@ angularDirective("ng:hide", function(expression, element){ angularDirective("ng:style", function(expression, element) { return function(element) { this.$watch(expression, function(scope, newStyles, oldStyles) { - if (oldStyles) forEach(oldStyles, function(val, style) { element.css(style, '');}); + if (oldStyles && (newStyles !== oldStyles)) { + forEach(oldStyles, function(val, style) { element.css(style, '');}); + } if (newStyles) element.css(newStyles); }); }; -- cgit v1.2.3