码迷,mamicode.com
首页 > 其他好文 > 详细

[Angular 2] Interpolation: check object exists

时间:2016-04-06 02:13:54      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

In Angular2, sometime we use @Output to pass data to parent component, then parent may pass the data down to another child component.

When you want to display the property of this object, we may need to check whether the object exists or not, otherwise if it not exists, angular2 will throw error.

 

Of course you can predefine object in the controller, or do something like:

<h2>{{thisHero && thisHero.name}}</h2>

Check the object first, then display the name.

 

In angular2 , there is another simple way to do it:

<h2>{{thisHero?.name}}</h2>

THe ? mark tell angular to check whether the object exists, if not, just ingore it

[Angular 2] Interpolation: check object exists

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5357646.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!