Skip to content

Commit 25c4380

Browse files
committed
feat($interpolate): add strict param to returned fn
1 parent fcc3a7a commit 25c4380

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/ng/interpolate.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -182,23 +182,29 @@ function $InterpolateProvider() {
182182
return concat.join('');
183183
};
184184

185-
var stringify = function (value) {
185+
var getValue = function (value) {
186186
if (trustedContext) {
187187
value = $sce.getTrusted(trustedContext, value);
188188
} else {
189189
value = $sce.valueOf(value);
190190
}
191191

192-
if (value === null || isUndefined(value)) {
192+
return value;
193+
};
194+
195+
var stringify = function (value) {
196+
if (isUndefined(value) || value === null) {
193197
value = '';
194-
} else if (typeof value != 'string') {
198+
}
199+
if (typeof value != 'string') {
195200
value = toJson(value);
196201
}
197202

198203
return value;
199204
};
200205

201-
return extend(function interpolationFn(context) {
206+
return extend(function interpolationFn(context, strict) {
207+
strict = !!strict;
202208
var scopeId = context.$id || 'notAScope';
203209
var lastValues = lastValuesCache.values[scopeId];
204210
var lastResult = lastValuesCache.results[scopeId];
@@ -225,7 +231,11 @@ function $InterpolateProvider() {
225231

226232
try {
227233
for (; i < ii; i++) {
228-
val = stringify(parseFns[i](context));
234+
val = getValue(parseFns[i](context));
235+
if (strict && isUndefined(val)) {
236+
return;
237+
}
238+
val = stringify(val);
229239
if (val !== lastValues[i]) {
230240
inputsChanged = true;
231241
}

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}}')({}, 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)