From 6b6f12c9204c800ece60e671023b3a6e5c77a3ea Mon Sep 17 00:00:00 2001 From: Jason Bedard Date: Sun, 16 Oct 2016 03:10:34 -0700 Subject: [PATCH] fix($parse): allow watching object/array literals --- src/ng/compile.js | 2 +- src/ng/parse.js | 4 ++-- test/ng/parseSpec.js | 23 +++++++++++++++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/ng/compile.js b/src/ng/compile.js index bc1cae5bfca6..0887a7086b10 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -3522,7 +3522,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { } recordChanges(scopeName, newValue, oldValue); destination[scopeName] = newValue; - }, deepWatch); + }); removeWatchCollection.push(removeWatch); break; diff --git a/src/ng/parse.js b/src/ng/parse.js index 3e2661e16f6d..fc62eea2839a 100644 --- a/src/ng/parse.js +++ b/src/ng/parse.js @@ -1826,7 +1826,7 @@ function $ParseProvider() { oldInputValueOf = newInputValue && getValueOf(newInputValue); } return lastResult; - }, listener, objectEquality, prettyPrintExpression); + }, listener, objectEquality || parsedExpression.literal, prettyPrintExpression); } var oldInputValueOfValues = []; @@ -1852,7 +1852,7 @@ function $ParseProvider() { } return lastResult; - }, listener, objectEquality, prettyPrintExpression); + }, listener, objectEquality || parsedExpression.literal, prettyPrintExpression); } function oneTimeWatchDelegate(scope, listener, objectEquality, parsedExpression, prettyPrintExpression) { diff --git a/test/ng/parseSpec.js b/test/ng/parseSpec.js index 6af4671b264f..58a3b7a56526 100644 --- a/test/ng/parseSpec.js +++ b/test/ng/parseSpec.js @@ -3117,6 +3117,29 @@ describe('parser', function() { scope.$digest(); expect(objB.value).toBe(scope.input); })); + + it('should support watching literals', inject(function($parse) { + var lastVal = NaN; + var callCount = 0; + var listener = function(val) { callCount++; lastVal = val; }; + + scope.$watch('{val: val}', listener); + + scope.$apply('val = 1'); + expect(callCount).toBe(1); + expect(lastVal).toEqual({val: 1}); + + scope.$apply('val = []'); + expect(callCount).toBe(2); + expect(lastVal).toEqual({val: []}); + + scope.$apply('val = []'); + expect(callCount).toBe(2); + + scope.$apply('val.push(1)'); + expect(callCount).toBe(3); + expect(lastVal).toEqual({val: [1]}); + })); }); describe('locals', function() {