Skip to content

Commit 97deb60

Browse files
committed
fix($parse): do evaluate several times expressions with interceptors
For simple expressions without filters that need a stateless interceptor then handle the 2nd phase parse evaluation using `inputs`. Closes angular#12983
1 parent dc818e1 commit 97deb60

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/ng/parse.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,7 @@ function getInputs(body) {
754754
var candidate = lastExpression.toWatch;
755755
if (candidate.length !== 1) return candidate;
756756
return candidate[0] !== lastExpression ? candidate : undefined;
757+
return lastExpression.toWatch;
757758
}
758759

759760
function isAssignable(ast) {
@@ -1906,13 +1907,14 @@ function $ParseProvider() {
19061907
function addInterceptor(parsedExpression, interceptorFn) {
19071908
if (!interceptorFn) return parsedExpression;
19081909
var watchDelegate = parsedExpression.$$watchDelegate;
1910+
var useInputs = false;
19091911

19101912
var regularWatch =
19111913
watchDelegate !== oneTimeLiteralWatchDelegate &&
19121914
watchDelegate !== oneTimeWatchDelegate;
19131915

19141916
var fn = regularWatch ? function regularInterceptedExpression(scope, locals, assign, inputs) {
1915-
var value = parsedExpression(scope, locals, assign, inputs);
1917+
var value = useInputs && inputs ? inputs[0] : parsedExpression(scope, locals, assign, inputs);
19161918
return interceptorFn(value, scope, locals);
19171919
} : function oneTimeInterceptedExpression(scope, locals, assign, inputs) {
19181920
var value = parsedExpression(scope, locals, assign, inputs);
@@ -1930,6 +1932,7 @@ function $ParseProvider() {
19301932
// If there is an interceptor, but no watchDelegate then treat the interceptor like
19311933
// we treat filters - it is assumed to be a pure function unless flagged with $stateful
19321934
fn.$$watchDelegate = inputsWatchDelegate;
1935+
useInputs = !parsedExpression.inputs;
19331936
fn.inputs = parsedExpression.inputs ? parsedExpression.inputs : [parsedExpression];
19341937
}
19351938

test/ng/compileSpec.js

+8
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,14 @@ describe('$compile', function() {
947947
expect(child).toHaveClass('log'); // merged from replace directive template
948948
}));
949949

950+
it('should interpolate the values once per digest',
951+
inject(function($compile, $rootScope, log) {
952+
element = $compile('<div>{{log("A")}} foo {{log("B")}}</div>')($rootScope);
953+
$rootScope.log = log;
954+
$rootScope.$digest();
955+
expect(log).toEqual('A; B; A; B');
956+
}));
957+
950958
it('should update references to replaced jQuery context', function() {
951959
module(function($compileProvider) {
952960
$compileProvider.directive('foo', function() {

0 commit comments

Comments
 (0)