-
Notifications
You must be signed in to change notification settings - Fork 26.2k
Angular Ivy causes objects not rendering correctly when implemented toString and valueOf #38839
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
As a temporary fix, couldn't you cast it to a number? E.g. |
not really, we got special case in our project, that some properties in objects pulled from api might be encrypted - this is also true for whole lists. Our goal is to display data with placeholders, without breaking layouts. Solution we came with was Proxied object. With these we could pass this special object inside *ngFor, and every reference to value or string was filled with placeholders, while subproperties was generated on fly. it worked flawlessy until we enabled angular ivy The idea was not to touch any templates, because we got over thousand of components by now, and handling this case everywhere would be catastrophic |
The reason it works this way is because we convert values to strings using |
Currently we convert objects to strings using `'' + value` which is quickest, but it stringifies the value using its `valueOf`, rather than `toString`. These changes switch to using `String(value)` which has identical performance and calls the `toString` method as expected. Note that another option was calling `toString` directly, but benchmarking showed it to be slower. I've included the benchmark I used to verify the performance so we have it for future reference and we can reuse it when making changes to `renderStringify` in the future. Also for reference, here are the results of the benchmark: ``` Benchmark: renderStringify concat: 2.006 ns(0%) concat with toString: 2.201 ns(-10%) toString: 237.494 ns(-11741%) toString with toString: 121.072 ns(-5937%) constructor: 2.201 ns(-10%) constructor with toString: 2.201 ns(-10%) toString mono: 14.536 ns(-625%) toString with toString mono: 9.757 ns(-386%) ``` Fixes angular#38839.
…39843) Currently we convert objects to strings using `'' + value` which is quickest, but it stringifies the value using its `valueOf`, rather than `toString`. These changes switch to using `String(value)` which has identical performance and calls the `toString` method as expected. Note that another option was calling `toString` directly, but benchmarking showed it to be slower. I've included the benchmark I used to verify the performance so we have it for future reference and we can reuse it when making changes to `renderStringify` in the future. Also for reference, here are the results of the benchmark: ``` Benchmark: renderStringify concat: 2.006 ns(0%) concat with toString: 2.201 ns(-10%) toString: 237.494 ns(-11741%) toString with toString: 121.072 ns(-5937%) constructor: 2.201 ns(-10%) constructor with toString: 2.201 ns(-10%) toString mono: 14.536 ns(-625%) toString with toString mono: 9.757 ns(-386%) ``` Fixes #38839. PR Close #39843
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Uh oh!
There was an error while loading. Please reload this page.
🐞 bug report
Affected Package
The issue is caused by package @angular/compilerIs this a regression?
Yes, the previous version in which this bug was not present was: any version with IVY disabled
Description
When IVY compiler is enabled, every object that is passed in interpollation marks tries to use .valueOf() method before .toString(), because of that it is not possible for creation of flexible objects that can take form of numbers if needed.
In previous version it was possible to create such constructions:
Also when passing Proxy object to template, console error is thrown. like it can not find toString method at all.
🔬 Minimal Reproduction
examples with IVY enabled and disabled:
https://stackblitz.com/edit/angular-ivy-af1zfj?file=src%2Fapp%2Fapp.component.ts
https://stackblitz.com/edit/angular-wwwg7j?file=src%2Fapp%2Fapp.component.html
🌍 Your Environment
Angular Version:
The text was updated successfully, but these errors were encountered: