Skip to content

Commit

Permalink
fix($parse): handle constants as one-time binding expressions
Browse files Browse the repository at this point in the history
Handle constant expressions as one-time binding expressions.
Avoids the infinite digest from
https://github.com/angular/angular.js/pull/7960/files#r14136938

Closes angular#7970
  • Loading branch information
lgalfaso authored and Cameron Knight committed Jul 16, 2014
1 parent f768c41 commit e30b34d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
8 changes: 2 additions & 6 deletions src/ng/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 () {
Expand Down
9 changes: 9 additions & 0 deletions test/ng/rootScopeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit e30b34d

Please sign in to comment.