在角度2中的字符串插值表达式中使用Elvis运算符 [英] Using Elvis Operator within String Interpolated Expressions in Angular 2

查看:93
本文介绍了在角度2中的字符串插值表达式中使用Elvis运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Angular 2应用程序中,我使用字符串插值从mongoDB中提取数据。现在服务器/数据库处于不稳定状态,因此有时会破坏我的客户端显示,例如,如果我通过字符串插值提取数据,如下所示:

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}}

...然后,稍后,数据库中数据的组织方式发生变化,这将打破我的显示 - 因为客户端正试图从不再存在的字段中拉入。所以我认为我应该能够在像这样的用例中使用像elvis运算符这样的东西。这样,如果数据是客户端查找的位置,它将打印到屏幕上。但是,如果数据不是它正在寻找的地方,它将完全忽略该字段 - 不会破坏显示中的任何内容。

... 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.

因此,简而言之,如何我在上面的表达式上实现了elvis运算符吗?

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

推荐答案

你可以这样使用它:

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

这将检查是否定义了记录然后地址[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

这篇关于在角度2中的字符串插值表达式中使用Elvis运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆