【问题标题】:Access nested properties in angular2 template访问 angular2 模板中的嵌套属性
【发布时间】:2026-01-24 14:45:02
【问题描述】:

在我的 angular2 模板中访问嵌套对象时遇到问题。错误:

错误:未捕获(承诺中):错误:/app/admin/property/items/property-items.html:7:52 中的错误,原因是:self.context.property.PropertyDataType 未定义

我的模板代码:

    <div class="col-xs-12 content-area" *ngIf="property.PropertyDataType.IsList == true">
    </div>

但是当我在组件中使用 console.log(object) 时,我可以访问所有属性。见截图:

我以为我可以通过模板中的“property.PropertyDataType.IsList”访问嵌套属性,但它似乎无法识别嵌套属性:PropertyDataType。

任何帮助将不胜感激。谢谢

【问题讨论】:

    标签: angular angular2-template


    【解决方案1】:

    您可以使用安全导航运算符来防止在 Angular 解析绑定时值尚未设置的错误:

    *ngIf="property?.PropertyDataType?.IsList == true"
    

    【讨论】:

    • 啊我忘了这个!谢谢