Skip to content

Commit

Permalink
fix($parse): evaluate once simple expressions in interpolations
Browse files Browse the repository at this point in the history
For simple expressions without filters that have a stateless interceptor
then handle the 2nd phase parse evaluation using `inputs`.

This fixes the issue that interpolated simple expressions were evaluated twice
within one digest loop.

Closes angular#12983
  • Loading branch information
lgalfaso committed Oct 3, 2015
1 parent dc818e1 commit bc2bd90
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/ng/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -1906,13 +1906,14 @@ function $ParseProvider() {
function addInterceptor(parsedExpression, interceptorFn) {
if (!interceptorFn) return parsedExpression;
var watchDelegate = parsedExpression.$$watchDelegate;
var useInputs = false;

var regularWatch =
watchDelegate !== oneTimeLiteralWatchDelegate &&
watchDelegate !== oneTimeWatchDelegate;

var fn = regularWatch ? function regularInterceptedExpression(scope, locals, assign, inputs) {
var value = parsedExpression(scope, locals, assign, inputs);
var value = useInputs && inputs ? inputs[0] : parsedExpression(scope, locals, assign, inputs);
return interceptorFn(value, scope, locals);
} : function oneTimeInterceptedExpression(scope, locals, assign, inputs) {
var value = parsedExpression(scope, locals, assign, inputs);
Expand All @@ -1930,6 +1931,7 @@ function $ParseProvider() {
// If there is an interceptor, but no watchDelegate then treat the interceptor like
// we treat filters - it is assumed to be a pure function unless flagged with $stateful
fn.$$watchDelegate = inputsWatchDelegate;
useInputs = !parsedExpression.inputs;
fn.inputs = parsedExpression.inputs ? parsedExpression.inputs : [parsedExpression];
}

Expand Down
8 changes: 8 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,14 @@ describe('$compile', function() {
expect(child).toHaveClass('log'); // merged from replace directive template
}));

it('should interpolate the values once per digest',
inject(function($compile, $rootScope, log) {
element = $compile('<div>{{log("A")}} foo {{::log("B")}}</div>')($rootScope);
$rootScope.log = log;
$rootScope.$digest();
expect(log).toEqual('A; B; A; B');
}));

it('should update references to replaced jQuery context', function() {
module(function($compileProvider) {
$compileProvider.directive('foo', function() {
Expand Down

0 comments on commit bc2bd90

Please sign in to comment.