为什么使用“?"模板绑定中的运算符? [英] Why use "?" operator in template binding?

查看:57
本文介绍了为什么使用“?"模板绑定中的运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用?时,绑定效果很好.如果删除它,则它在视图中不会显示任何内容.

When I use ?, the binding works well. If I remove it, it doesn't show anything in the view.

<span class="subhead">{{project?.category}}</span>

你能告诉我区别吗?以这种方式使用它是一种好习惯吗?

Can you please tell me the difference? Is it a good practice to use it this way?

推荐答案

当Angular在project被赋值之前渲染视图时,会导致异常.当projectnullundefined时,?.停止评估,这通常发生在异步获取数据(例如从服务器获取数据)时,这种情况可能会花费一些时间.

When Angular renders the view before project got a value assigned, it causes an exception. ?. stops evaluating when project is null or undefined, which usually happens when data is fetched async, for example from the server which can take quite some time.

下次更改检测识别到更改时,将重新评估绑定.当project具有值时,它将绑定project.category.

The next time change detection recognizes a change, the bindings will be re-evaluated. When project then has a value it will bind project.category.

这篇关于为什么使用“?"模板绑定中的运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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