角度类型错误:无法读取未定义的“名称”属性

yam*_*585 1 typeahead angular

我正在开发 angular 2 release 6 应用程序和以下代码行:

<input #instance="ngbTypeahead" type="text" class="form-control" [(ngModel)]="item.outputProjection.name" [ngbTypeahead]="search" />   
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

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)
Run Code Online (Sandbox Code Playgroud)

而且我不确定如何更好地检查引导程序预先输入的对象定义。

Din*_*ong 5

请确保item.outputProjection不是undefined

您的解决方法是item.outputProjection = {}在您的组件.ts文件中定义。