[英]angular TypeError: Cannot read property of 'name' undefined
我正在使用Angle 2 Release 6應用程序和以下代碼行:
<input #instance="ngbTypeahead" type="text" class="form-control" [(ngModel)]="item.outputProjection.name" [ngbTypeahead]="search" />
並且出現以下錯誤:
DatacartComponent.html:60 ERROR TypeError: Cannot read property 'name' of undefined
at Object.eval [as updateDirectives] (DatacartComponent.html:60)
at Object.debugUpdateDirectives [as updateDirectives] (core.js:11914)
at checkAndUpdateView (core.js:11307)
at callViewAction (core.js:11548)
at execEmbeddedViewsAction (core.js:11511)
at checkAndUpdateView (core.js:11308)
at callViewAction (core.js:11548)
at execEmbeddedViewsAction (core.js:11511)
at checkAndUpdateView (core.js:11308)
at callViewAction (core.js:11548)
而且我不太確定該如何做才能更好地檢查預引導類型上的對象定義。
請確保undefined
item.outputProjection
。
解決方法是在組件.ts
文件中定義item.outputProjection = {}
。
你可以試試這個嗎
<input #instance="ngbTypeahead" type="text" class="form-control" [(ngModel)]="item.outputProjection?.name" [ngbTypeahead]="search" />
item.outputProjection?.name不會拋出未定義的錯誤,它將檢查是否定義了item.outputProjection,如果是,則從中獲取名稱。
不幸的是,我無法將Elvis運算符添加到具有2種方式綁定的ngModel中,所以我做到了這一點,並且有效。
[(ngModel)]="item && item.outputProjection && item.outputProjection.name"
而且有效!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.