angular2 中的空数组中的模板/渲染属性? [英] Templating/rendering properties in an empty array in angular2?

查看:32
本文介绍了angular2 中的空数组中的模板/渲染属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://jsfiddle.net/48yh14c3/

this.list = [{属性:[{anotherProp: true}]},{财产: []},{属性:[{anotherProp: false}]}]

在 angular 1 中,您可以引用深层属性,并且(在大多数情况下)它会继续摇摆:

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

我确定我可以 *ngIf 父属性来确保它存在,或者扁平化原始 POJO.只是想知道我是否遗漏了什么?

解决方案

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

{{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

这篇关于angular2 中的空数组中的模板/渲染属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆