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

Interpolation of Date object produces quoted ISO string (v1.4.4). #14027

Closed
sthames42 opened this issue Feb 12, 2016 · 4 comments
Closed

Interpolation of Date object produces quoted ISO string (v1.4.4). #14027

sthames42 opened this issue Feb 12, 2016 · 4 comments

Comments

@sthames42
Copy link

Building a directive that takes a date string for an attribute. If bound to a Date object, the interpolated value passed to the $observe/$watch function is an ISO string surrounded by double quotes.

Markup:

  <my-directive today="{{selectedDate}}"/>

link method:

  attrs.$observe('today', function(val) { opts.today  = new Date(val); });

Value of 'val' received in function and viewed in console looks like:

  ""2016-02-12T22:59:26.630Z""

This can be resolved with opts.today = new Date(eval(val)) but it would be necessary to see if someone actually supplied a static string date for the attribute instead of a bound object by checking for a leading quote.

Is this working as designed or is it a bug?

@gkalpak
Copy link
Member

gkalpak commented Feb 13, 2016

Yes, this is expected behavior, since you are using interpolation ({{...}}).
$interpolate must return a string adnd thus tries to stringify non-string values, by converting them to JSON.

If you want to pass the value as object, you can use two-way binding (=) - or as of v1.5.0 one-way-binding (<).

Closing as this works as expected.

@gkalpak gkalpak closed this as completed Feb 13, 2016
@sthames42
Copy link
Author

Thank you for clarification. I was finally able to find some reference to AngularJS calling the toJSON() method on objects if it exists as the default behavior. Nothing in $interpolate documentation that I could find.

@gkalpak
Copy link
Member

gkalpak commented Feb 13, 2016

As mentioned in the docs, $interpolate returns a function that returns a string, so it obviously needs to convert non-string values to string. It does so, calling angular.toJson(), which calls JSON.stringify() under the hood.

Calling an object's toJSON() method (if it exists) is done by JSON.stringify() and is not Angular-related (it's plain old native JS).

I agree a notice in the $interpolate docs, mentioning that non-string values are JSONified, would be in place.
PRs welcome 😉

@sthames42
Copy link
Author

If you mean "an interpolation function which is used to compute the interpolated string. The function has these parameters," the only reference in the $interpolate docs I could find, I don't even know what it means. However, I am thoroughly familiar with string interpolation having worked extensively with many languages that employ it and AngularJS is the only one I've seen in which stringification of an object returns the object serialized to a JSON string. This is not even consistent with Javascript which returns a call to the toString() method.

But this is clearly being addressed in another issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants