From 4b8bbf87ce26e046aaa7510568f254b480f44804 Mon Sep 17 00:00:00 2001 From: Kindy Lin Date: Tue, 24 Jan 2017 15:54:53 +0800 Subject: [PATCH 1/2] fix($parse): Make sure ES6 object computed property to be watched Adding the missing watches for ES6 object property which added in #14407 --- src/ng/parse.js | 7 +++++++ test/ng/parseSpec.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/ng/parse.js b/src/ng/parse.js index 8d4fc5106c11..3a08d8b900a1 100644 --- a/src/ng/parse.js +++ b/src/ng/parse.js @@ -815,6 +815,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 3bf4521b3645..62d08a4341be 100644 --- a/test/ng/parseSpec.js +++ b/test/ng/parseSpec.js @@ -3868,6 +3868,41 @@ describe('parser', function() { scope.$digest(); expect(objB.value).toBe(scope.input); })); + + it('should watch ES6 object computed property changes', function() { + var count = 0; + var values = []; + var firstValue = {'undefined': true}; + + scope.$watch('{[a]: true}', function(val, oldVal) { + count++; + values.push(val); + }, true); + + scope.$digest(); + expect(count).toBe(1); + expect(values[0]).toEqual(firstValue); + + scope.$digest(); + expect(count).toBe(1); + expect(values[0]).toEqual(firstValue); + + 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}); + }); + }); describe('locals', function() { From 72787626a0e9d65de8a51a5a6ef7bd3ed713cf74 Mon Sep 17 00:00:00 2001 From: Kindy Lin Date: Wed, 25 Jan 2017 10:18:55 +0800 Subject: [PATCH 2/2] fix($parse): fixup: follow code style --- test/ng/parseSpec.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/test/ng/parseSpec.js b/test/ng/parseSpec.js index 62d08a4341be..1df4a3135690 100644 --- a/test/ng/parseSpec.js +++ b/test/ng/parseSpec.js @@ -3872,20 +3872,19 @@ describe('parser', function() { it('should watch ES6 object computed property changes', function() { var count = 0; var values = []; - var firstValue = {'undefined': true}; - scope.$watch('{[a]: true}', function(val, oldVal) { + scope.$watch('{[a]: true}', function(val) { count++; values.push(val); }, true); scope.$digest(); expect(count).toBe(1); - expect(values[0]).toEqual(firstValue); + expect(values[0]).toEqual({'undefined': true}); scope.$digest(); expect(count).toBe(1); - expect(values[0]).toEqual(firstValue); + expect(values[0]).toEqual({'undefined': true}); scope.a = true; scope.$digest();