From 604418ab8c1ea7924015b8518d277a8b7ed4c997 Mon Sep 17 00:00:00 2001 From: Kindy Lin Date: Tue, 24 Jan 2017 15:54:53 +0800 Subject: [PATCH] fix($parse): Make sure ES6 object computed property to be watched Adding the missing watches for ES6 object property which added in #14407 (cherry picked from commit 4b8bbf87ce26e046aaa7510568f254b480f44804) (and also apply commit 72787626a0e9d65de8a51a5a6ef7bd3ed713cf74) --- src/ng/parse.js | 7 +++++++ test/ng/parseSpec.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/ng/parse.js b/src/ng/parse.js index ba0ba339c868..98179e8f549b 100644 --- a/src/ng/parse.js +++ b/src/ng/parse.js @@ -717,6 +717,13 @@ function findConstantAndWatchExpressions(ast, $filter) { if (!property.value.constant) { argsToWatch.push.apply(argsToWatch, property.value.toWatch); } + if (property.computed) { + findConstantAndWatchExpressions(property.key, $filter); + if (!property.key.constant) { + argsToWatch.push.apply(argsToWatch, property.key.toWatch); + } + } + }); ast.constant = allConstants; ast.toWatch = argsToWatch; diff --git a/test/ng/parseSpec.js b/test/ng/parseSpec.js index c815bb514261..dbbb2237ad84 100644 --- a/test/ng/parseSpec.js +++ b/test/ng/parseSpec.js @@ -3119,6 +3119,39 @@ describe('parser', function() { expect(objB.value).toBe(scope.input); })); + it('should watch ES6 object computed property changes', function() { + var count = 0; + var values = []; + + scope.$watch('{[a]: true}', function(val) { + count++; + values.push(val); + }, true); + + scope.$digest(); + expect(count).toBe(1); + expect(values[0]).toEqual({'undefined': true}); + + scope.$digest(); + expect(count).toBe(1); + expect(values[0]).toEqual({'undefined': true}); + + scope.a = true; + scope.$digest(); + expect(count).toBe(2); + expect(values[1]).toEqual({'true': true}); + + scope.a = 'abc'; + scope.$digest(); + expect(count).toBe(3); + expect(values[2]).toEqual({'abc': true}); + + scope.a = undefined; + scope.$digest(); + expect(count).toBe(4); + expect(values[3]).toEqual({'undefined': true}); + }); + it('should support watching literals', inject(function($parse) { var lastVal = NaN; var callCount = 0;