aboutsummaryrefslogtreecommitdiffstats
path: root/src/Angular.js
diff options
context:
space:
mode:
authorCodier2011-11-15 18:45:36 -0600
committerIgor Minar2011-11-21 15:43:12 -0800
commit29f9e2665d8b771a6226870fc8fd2c4c94d7a2c0 (patch)
treec48f91fea9b97b10b13d4e1ec636e6cd7995578e /src/Angular.js
parent8d1944851d5bbecd9277ede8c4a354c2d43796ee (diff)
downloadangular.js-29f9e2665d8b771a6226870fc8fd2c4c94d7a2c0.tar.bz2
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
Diffstat (limited to 'src/Angular.js')
-rw-r--r--src/Angular.js3
1 files changed, 2 insertions, 1 deletions
diff --git a/src/Angular.js b/src/Angular.js
index 127be95d..e5f35bba 100644
--- a/src/Angular.js
+++ b/src/Angular.js
@@ -624,6 +624,7 @@ function copy(source, destination){
*
* * Both objects or values pass `===` comparison.
* * Both objects or values are of the same type and all of their properties pass `===` comparison.
+ * * Both values are NaN. (In JavasScript, NaN == NaN => false. But we consider two NaN as equal)
*
* During a property comparision, properties of `function` type and properties with names
* that begin with `$` are ignored.
@@ -634,11 +635,11 @@ function copy(source, destination){
* @param {*} o1 Object or value to compare.
* @param {*} o2 Object or value to compare.
* @returns {boolean} True if arguments are equal.
- *
*/
function equals(o1, o2) {
if (o1 === o2) return true;
if (o1 === null || o2 === null) return false;
+ if (o1 !== o1 && o2 !== o2) return true; // NaN === NaN
var t1 = typeof o1, t2 = typeof o2, length, key, keySet;
if (t1 == t2 && t1 == 'object') {
if (isArray(o1)) {