Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit c2362e3

Browse files
committed
feat($interpolate): add optional allOrNothing param
1 parent 2b6c2c5 commit c2362e3

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

src/ng/interpolate.js

+37-5
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,26 @@ function $InterpolateProvider() {
105105
* expect(exp({name:'Angular'}).toEqual('Hello ANGULAR!');
106106
* ```
107107
*
108+
* `$interpolate` takes an optional fourth argument, `allOrNothing`. If `allOrNothing` is
109+
* `true`, the interpolation function will return `undefined` unless all embedded expressions
110+
* evaluate to a value other than `undefined`.
111+
*
112+
* ```js
113+
* var $interpolate = ...; // injected
114+
* var context = {greeting: 'Hey', name: undefined };
115+
*
116+
* // default "forgiving" mode
117+
* var exp = $interpolate('{{greeting}} {{name}}!');
118+
* expect(exp(context)).toEqual('Hello !');
119+
*
120+
* // "allOrNothing" mode
121+
* exp = $interpolate('{{greeting}} {{name}}!', false, null, true);
122+
* expect(exp(context, true)).toBeUndefined();
123+
* context.name = 'Angular';
124+
* expect(exp(context, true)).toEqual('Hello Angular!');
125+
* ```
126+
*
127+
* `allOrNothing` is useful for interpolating URLs. `ngSrc` and `ngSrcset` use this behavior.
108128
*
109129
* @param {string} text The text with markup to interpolate.
110130
* @param {boolean=} mustHaveExpression if set to true then the interpolation string must have
@@ -114,12 +134,15 @@ function $InterpolateProvider() {
114134
* result through {@link ng.$sce#getTrusted $sce.getTrusted(interpolatedResult,
115135
* trustedContext)} before returning it. Refer to the {@link ng.$sce $sce} service that
116136
* provides Strict Contextual Escaping for details.
137+
* @param {boolean=} allOrNothing if `true`, then the returned function returns undefined
138+
* unless all embedded expressions evaluate to a value other than `undefined`.
117139
* @returns {function(context)} an interpolation function which is used to compute the
118140
* interpolated string. The function has these parameters:
119141
*
120142
* - `context`: evaluation context for all expressions embedded in the interpolated text
121143
*/
122-
function $interpolate(text, mustHaveExpression, trustedContext) {
144+
function $interpolate(text, mustHaveExpression, trustedContext, allOrNothing) {
145+
allOrNothing = !!allOrNothing;
123146
var startIndex,
124147
endIndex,
125148
index = 0,
@@ -182,16 +205,21 @@ function $InterpolateProvider() {
182205
return concat.join('');
183206
};
184207

185-
var stringify = function (value) {
208+
var getValue = function (value) {
186209
if (trustedContext) {
187210
value = $sce.getTrusted(trustedContext, value);
188211
} else {
189212
value = $sce.valueOf(value);
190213
}
191214

192-
if (value === null || isUndefined(value)) {
215+
return value;
216+
};
217+
218+
var stringify = function (value) {
219+
if (isUndefined(value) || value === null) {
193220
value = '';
194-
} else if (typeof value != 'string') {
221+
}
222+
if (typeof value != 'string') {
195223
value = toJson(value);
196224
}
197225

@@ -225,7 +253,11 @@ function $InterpolateProvider() {
225253

226254
try {
227255
for (; i < ii; i++) {
228-
val = stringify(parseFns[i](context));
256+
val = getValue(parseFns[i](context));
257+
if (allOrNothing && isUndefined(val)) {
258+
return;
259+
}
260+
val = stringify(val);
229261
if (val !== lastValues[i]) {
230262
inputsChanged = true;
231263
}

test/ng/interpolateSpec.js

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ describe('$interpolate', function() {
1919
expect($interpolate('some text', true)).toBeUndefined();
2020
}));
2121

22+
it('should return undefined when there are bindings and strict is set to true',
23+
inject(function($interpolate) {
24+
expect($interpolate('test {{foo}}', false, null, true)({})).toBeUndefined();
25+
}));
26+
2227
it('should suppress falsy objects', inject(function($interpolate) {
2328
expect($interpolate('{{undefined}}')({})).toEqual('');
2429
expect($interpolate('{{null}}')({})).toEqual('');

0 commit comments

Comments
 (0)