Skip to content

Commit 9c3bff2

Browse files
committed
feat($interpolate): use custom toString member if present
BREAKING CHANGE: use custom toString implementation when present on object value types. Behavior is consistent with implementations found in other languages such as Ruby, Python, and CoffeeScript. http://rubymonk.com/learning/books/1-ruby-primer/chapters/5-strings/lessons/31-string-basics https://docs.python.org/2/library/stdtypes.html#string-formatting-operations http://coffeescriptcookbook.com/chapters/strings/interpolation Closes angular#7317
1 parent f684c21 commit 9c3bff2

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/ng/interpolate.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,11 @@ function $InterpolateProvider() {
272272
break;
273273
}
274274
default: {
275-
value = toJson(value);
275+
if(isFunction(value.toString) && value.toString !== Object.prototype.toString && !isArray(value)) {
276+
value = value.toString();
277+
} else {
278+
value = toJson(value);
279+
}
276280
}
277281
}
278282

test/ng/interpolateSpec.js

+15
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ describe('$interpolate', function() {
3636
expect($interpolate('{{ false }}')({})).toEqual('false');
3737
}));
3838

39+
it('should use custom toString when present', inject(function($interpolate, $rootScope) {
40+
var scope = $rootScope.$new();
41+
scope.a = {
42+
toString: function() {
43+
return 'foo';
44+
}
45+
};
46+
47+
expect($interpolate('{{ a }}')(scope)).toEqual('foo');
48+
}));
49+
50+
it('should NOT use toString on array objects', inject(function($interpolate) {
51+
expect($interpolate('{{a}}')({ a: [] })).toEqual('[]');
52+
}));
53+
3954

4055
it('should return interpolation function', inject(function($interpolate, $rootScope) {
4156
var interpolateFn = $interpolate('Hello {{name}}!');

0 commit comments

Comments
 (0)