@@ -114,12 +114,15 @@ function $InterpolateProvider() {
114114 * result through {@link ng.$sce#getTrusted $sce.getTrusted(interpolatedResult,
115115 * trustedContext)} before returning it. Refer to the {@link ng.$sce $sce} service that
116116 * provides Strict Contextual Escaping for details.
117- * @returns {function(context) } an interpolation function which is used to compute the
118- * interpolated string. The function has these parameters:
117+ * @returns {Object } An object describing the interpolation template string.
119118 *
120- * * `context`: an object against which any expressions embedded in the strings are evaluated
121- * against.
119+ * The properties of the returned object include:
122120 *
121+ * - `template` — `{string}` — original interpolation template string.
122+ * - `separators` — `{Array.<string>}` — array of separators extracted from the template.
123+ * - `expressions` — `{Array.<string>}` — array of expressions extracted from the template.
124+ * - `compute` — {function(Array)()} — function that when called with an array of values will
125+ * compute the result of interpolation for the given interpolation template and values.
123126 */
124127 function $interpolate ( text , mustHaveExpression , trustedContext ) {
125128 var startIndex ,
@@ -139,8 +142,8 @@ function $InterpolateProvider() {
139142 ( ( endIndex = text . indexOf ( endSymbol , startIndex + startSymbolLength ) ) != - 1 ) ) {
140143 if ( index !== startIndex ) hasText = true ;
141144 separators . push ( text . substring ( index , startIndex ) ) ;
142- expressions . push ( fn = interpolateParse ( exp = text . substring ( startIndex + startSymbolLength , endIndex ) ) ) ;
143- fn . exp = exp ;
145+ exp = text . substring ( startIndex + startSymbolLength , endIndex ) ;
146+ expressions . push ( exp ) ;
144147 index = endIndex + endSymbolLength ;
145148 hasInterpolation = true ;
146149 } else {
@@ -172,55 +175,51 @@ function $InterpolateProvider() {
172175
173176 if ( ! mustHaveExpression || hasInterpolation ) {
174177 concat . length = separators . length + expressions . length ;
175- var computeFn = function ( values , context ) {
176- for ( var i = 0 , ii = expressions . length ; i < ii ; i ++ ) {
177- concat [ 2 * i ] = separators [ i ] ;
178- concat [ ( 2 * i ) + 1 ] = values ? values [ i ] : expressions [ i ] ( context ) ;
178+
179+ return extend ( function interpolationFn ( scope ) {
180+ var values = [ ] ;
181+ forEach ( interpolationFn . expressions , function ( expression ) {
182+ values . push ( scope . $eval ( expression ) ) ;
183+ } ) ;
184+ return interpolationFn . compute ( values ) ;
185+ } , {
186+ exp : text , //deprecated
187+ template : text ,
188+ separators : separators ,
189+ expressions : expressions ,
190+ compute : function ( values ) {
191+ for ( var i = 0 , ii = expressions . length ; i < ii ; i ++ ) {
192+ concat [ 2 * i ] = separators [ i ] ;
193+ concat [ ( 2 * i ) + 1 ] = stringify ( values [ i ] ) ;
194+ }
195+ concat [ 2 * ii ] = separators [ ii ] ;
196+ return concat . join ( '' ) ;
179197 }
180- concat [ 2 * ii ] = separators [ ii ] ;
181- return concat . join ( '' ) ;
182- } ;
198+ } ) ;
199+ }
183200
184- fn = function ( context ) {
185- return computeFn ( null , context ) ;
186- } ;
187- fn . exp = text ;
201+ function stringify ( value ) {
202+ try {
188203
189- // hack in order to preserve existing api
190- fn . $$invoke = function ( listener ) {
191- return function ( values , oldValues , scope ) {
192- var current = computeFn ( values , scope ) ;
193- listener ( current , this . $$lastInter == null ? current : this . $$lastInter , scope ) ;
194- this . $$lastInter = current ;
195- } ;
196- } ;
197- fn . separators = separators ;
198- fn . expressions = expressions ;
199- return fn ;
200- }
204+ if ( trustedContext ) {
205+ value = $sce . getTrusted ( trustedContext , value ) ;
206+ } else {
207+ value = $sce . valueOf ( value ) ;
208+ }
201209
202- function interpolateParse ( expression ) {
203- var exp = $parse ( expression ) ;
204- return function ( scope ) {
205- try {
206- var value = exp ( scope ) ;
207- if ( trustedContext ) {
208- value = $sce . getTrusted ( trustedContext , value ) ;
209- } else {
210- value = $sce . valueOf ( value ) ;
211- }
212- if ( value === null || isUndefined ( value ) ) {
213- value = '' ;
214- } else if ( typeof value != 'string' ) {
215- value = toJson ( value ) ;
216- }
217- return value ;
218- } catch ( err ) {
219- var newErr = $interpolateMinErr ( 'interr' , "Can't interpolate: {0}\n{1}" , text ,
220- err . toString ( ) ) ;
221- $exceptionHandler ( newErr ) ;
210+ if ( value === null || isUndefined ( value ) ) {
211+ value = '' ;
212+ } else if ( typeof value != 'string' ) {
213+ value = toJson ( value ) ;
222214 }
223- } ;
215+
216+ return value ;
217+
218+ } catch ( err ) {
219+ var newErr = $interpolateMinErr ( 'interr' , "Can't interpolate: {0}\n{1}" , text ,
220+ err . toString ( ) ) ;
221+ $exceptionHandler ( newErr ) ;
222+ }
224223 }
225224 }
226225
0 commit comments