且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

angular2 中的空数组中的模板/渲染属性?

更新时间:2023-11-26 16:02:22

是的,您缺少 猫王运算符:

{{thing.property[0]?.anotherProp}}

Plunker

http://jsfiddle.net/48yh14c3/

this.list = [
 {
  property: [{anotherProp: true}]
 },
 {
  property: []
 },
 {
  property: [{anotherProp: false}]
 }
]

In angular 1 you could reference deep properties and (for the most part) it would keep on rocking:

<div ng-repeat='thing in ctrl.list'>
   {{thing.property[0].anotherProp}}
</div>

I'm sure I could just *ngIf the parent property to make sure it exists, or flatten the original POJO. Just wondering if I'm missing something?

Yes, you're missing the Elvis operator:

<div *ngFor='#thing of list'>
   {{thing.property[0]?.anotherProp}}
</div>

Plunker