[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

你可能感兴趣的:([Angular 2] Interpolation: check object exists)