@@ -130,7 +130,8 @@ function $InterpolateProvider() {
130
130
hasInterpolation = false ,
131
131
hasText = false ,
132
132
exp ,
133
- concat = [ ] ;
133
+ concat = [ ] ,
134
+ lastValuesCache = { values : { } , results : { } } ;
134
135
135
136
while ( index < textLength ) {
136
137
if ( ( ( startIndex = text . indexOf ( startSymbol , index ) ) != - 1 ) &&
@@ -172,9 +173,6 @@ function $InterpolateProvider() {
172
173
if ( ! mustHaveExpression || hasInterpolation ) {
173
174
concat . length = separators . length + expressions . length ;
174
175
175
- var lastValues = [ ] ;
176
- var lastResult ;
177
-
178
176
var compute = function ( values ) {
179
177
for ( var i = 0 , ii = expressions . length ; i < ii ; i ++ ) {
180
178
concat [ 2 * i ] = separators [ i ] ;
@@ -201,12 +199,30 @@ function $InterpolateProvider() {
201
199
} ;
202
200
203
201
return extend ( function interpolationFn ( context ) {
202
+ var scopeId = context . $id || 'notAScope' ;
203
+ var lastValues = lastValuesCache . values [ scopeId ] ;
204
+ var lastResult = lastValuesCache . results [ scopeId ] ;
204
205
var i = 0 ;
205
206
var ii = expressions . length ;
206
207
var values = new Array ( ii ) ;
207
208
var val ;
208
209
var inputsChanged = lastResult === undefined ? true : false ;
209
210
211
+
212
+ // if we haven't seen this context before, initialize the cache and try to setup
213
+ // a cleanup routine that purges the cache when the scope goes away.
214
+ if ( ! lastValues ) {
215
+ lastValues = [ ] ;
216
+ inputsChanged = true ;
217
+ if ( context . $on ) {
218
+ context . $on ( '$destroy' , function ( ) {
219
+ lastValuesCache . values [ scopeId ] = null ;
220
+ lastValuesCache . results [ scopeId ] = null ;
221
+ } ) ;
222
+ }
223
+ }
224
+
225
+
210
226
try {
211
227
for ( ; i < ii ; i ++ ) {
212
228
val = stringify ( parseFns [ i ] ( context ) ) ;
@@ -217,8 +233,8 @@ function $InterpolateProvider() {
217
233
}
218
234
219
235
if ( inputsChanged ) {
220
- lastValues = values ;
221
- lastResult = compute ( values ) ;
236
+ lastValuesCache . values [ scopeId ] = values ;
237
+ lastValuesCache . results [ scopeId ] = lastResult = compute ( values ) ;
222
238
}
223
239
} catch ( err ) {
224
240
var newErr = $interpolateMinErr ( 'interr' , "Can't interpolate: {0}\n{1}" , text ,
0 commit comments