From d9763f1bd355190b9d4e5723e4632cbc232f0543 Mon Sep 17 00:00:00 2001 From: Lucas Galfaso Date: Tue, 24 Jun 2014 18:33:51 +0200 Subject: [PATCH] fix($parse): handle constants as one-time binding expressions Handle constant expressions as one-time binding expressions. Avoids the infinite digest from https://github.com/angular/angular.js/pull/7960/files#r14136938 Closes #7970 --- src/ng/parse.js | 8 ++------ test/ng/rootScopeSpec.js | 9 +++++++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/ng/parse.js b/src/ng/parse.js index 88d4c9f5f790..1ae26404e610 100644 --- a/src/ng/parse.js +++ b/src/ng/parse.js @@ -1027,11 +1027,7 @@ function $ParseProvider() { cache[exp] = parsedExpression; } - if (parsedExpression.constant) { - parsedExpression.$$unwatch = true; - } - - return oneTime ? oneTimeWrapper(parsedExpression) : parsedExpression; + return oneTime || parsedExpression.constant ? oneTimeWrapper(parsedExpression) : parsedExpression; case 'function': return exp; @@ -1050,7 +1046,7 @@ function $ParseProvider() { function oneTimeParseFn(self, locals) { if (!stable) { - lastValue = expression(self, locals); + lastValue = expression.constant && lastValue ? lastValue : expression(self, locals); oneTimeParseFn.$$unwatch = isDefined(lastValue); if (oneTimeParseFn.$$unwatch && self && self.$$postDigestQueue) { self.$$postDigestQueue.push(function () { diff --git a/test/ng/rootScopeSpec.js b/test/ng/rootScopeSpec.js index 1007e9760f28..adafaed5d8ae 100644 --- a/test/ng/rootScopeSpec.js +++ b/test/ng/rootScopeSpec.js @@ -114,6 +114,15 @@ describe('Scope', function() { expect($rootScope.$$watchers.length).toEqual(0); })); + it('should not keep constant literals on the watch queue', inject(function($rootScope) { + $rootScope.$watch('[]', function() {}); + $rootScope.$watch('{}', function() {}); + expect($rootScope.$$watchers.length).toEqual(2); + $rootScope.$digest(); + + expect($rootScope.$$watchers.length).toEqual(0); + })); + it('should clean up stable watches on the watch queue', inject(function($rootScope, $parse) { $rootScope.$watch($parse('::foo'), function() {}); expect($rootScope.$$watchers.length).toEqual(1);