NjMinko NjMinko - 2 months ago 9
JSON Question

How to use ngfor to print property of object?

<div *ngFor="let article of articleList;let i = index">
<div class="item">
<div class="image-holder"
style="background-image: url(https://c9.io/blog/content/images/2015/07/ninja-1.jpg)">
<a href="https://c9.io/blog/title-wip-cloud9-shortcutfoo/"></a>
</div>
<div class="title">
<a class="title-ref" href="#">{{article.title}}</a>
</div>
<div class="text">
<div>
{{article.description}} <a class="text-ref" href="#">>></a>
</div>
</div>
<div class="item-header">
<div class="author">
<img class="img-circle"
src="https://c9.io/blog/content/images/2015/05/brady.jpg" />
</div>
<span class="author-name"><a class="user-ref" href="#">{{article.user.name}}</a></span>
<div class="time">2015-07-31</div>
</div>
</div>
</div>


I can see the
article.title
, desprition etc. but the article.user.name is undefined. if
{{article.user}}
, it will show object.

How to get print
article.user.name
properly?

Answer

I think that you should use the safe navigation operator like this-

{{article?.user?.name}}

This link could give you more details: https://angular.io/docs/ts/latest/guide/template-syntax.html. See the section "safe navigation operator ( ?. ) and null property paths"

See if this helps.