You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
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?
The text was updated successfully, but these errors were encountered:
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 (<).
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.
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 😉
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.
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:
link method:
Value of 'val' received in function and viewed in console looks like:
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?
The text was updated successfully, but these errors were encountered: