是什么?在角度5中意味着什么? [英] What does ?. mean in angular 5?

查看:74
本文介绍了是什么?在角度5中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如果我有类似product.id == 1的东西吗?东西:不是东西.这意味着如果id = 1,则选择填充".如果不是,则选择不是东西".以下是什么意思:

I know the if I have something like product.id == 1 ? stuff : not stuff. That means if id = 1, then choose "stuff". If not, then choose "not stuff". What does the following mean:

product?.id.name

推荐答案

? 表示安全 来自 Docs

From Docs

Angular安全导航操作符(?.)流畅且方便 防止属性路径中的空值和未定义值的方法.这里 是的,如果currentHero为 空.

The Angular safe navigation operator (?.) is a fluent and convenient way to guard against null and undefined values in property paths. Here it is, protecting against a view render failure if the currentHero is null.

这特别意味着,如果您绑定到视图的值是null,那么它应该返回null,否则返回实际值,这样在渲染模板时不会出现任何问题.

This specifically means that if the value you are binding to the view is null then it should return null else the actual value, so that you dont get any issues while rendering the template.

在您提供的上述示例代码中,

In the above sample code you provided ,

product?.id.name

product?.id.name

它检查生产对象是否存在,然后将检查是否有一个id. 因为您在id后没有 ? . it will throw an error "cannot read property of 'name' undefined".

it checks whether produce object exists and then it will check if there is an id. since you dont have ? after id. it will throw an error "cannot read property of 'name' undefined".

这篇关于是什么?在角度5中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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