Using Elvis Operator within String Interpolated Expressions in Angular 2

穿精又带淫゛_ 提交于 2019-12-10 17:13:06

问题


In my Angular 2 app I am using string interpolation to pull data from a mongoDB. Right now the server/database in in flux, so on occasion that will break my client-side display because, for instance, if I am pulling in data via string interpolation, like this:

{{record.addresses[0].zipCode}}

... and then, at a later time, the way the data is organized in the database changes, this will break my display - because the client is trying to pull in from fields that are no longer there. So I think I should be able to use something like the elvis operator in a use case like this. That way, if the data is where the client is looking for it, it will print out to the screen. But, if the data is not where it's looking for it, it will just ignore the field altogether - not breaking anything in the display.

So, in short, how would I implement the elvis operator on an expression like I have above?


回答1:


You use it like this:

{{record?.addresses[0]?.zipCode}}

This will check if record is defined then if addresses[0] is defined under record object and then if zipCode is defined under that object



来源:https://stackoverflow.com/questions/41620320/using-elvis-operator-within-string-interpolated-expressions-in-angular-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!