[Angular 2] Interpolation: check object exists

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

上一篇:一个c++给程序打log的单例模式类


下一篇:佛山Uber优步司机奖励政策(1月25日~1月31日)