2017-01-24 85 views
-1

剛剛接觸angularjs。請看看我的代碼甚至不能知道該如何解釋這一Angularjs 2嵌套接口

接口

export interface ScanuploadInterface { 
    docVia string;customFields:Array<any>; 
    associateDocs :Array<any>; 
} 

模式

import {ScanuploadInterface} from '../../interfaces/scanupload.interface'; 
holeDocument = <ScanuploadInterface>{}; 

HTML:* ngFor動態輸入字段

<input id="{{list.fieldName}}" name="{{list.fieldName}}" type="{{list.fieldType}}" class="form-control" [(ngModel)]="holeDocument.customFields[list.fieldName]" > 

但是這一次拋出一個錯誤的

無法讀取的不確定財產 'field12'

field12是一個動態的價值來自list.fieldName。 我不知道如何解決這個問題。任何類型的解決方案,讚賞。提前致謝。

+0

你在哪裏初始化你的customFields?它說holeDocument.customFields在我的接口未定義 –

+0

,看到我的接口頭2號線,並在模型進口看第一行。@ AliBaig – sibi

+0

這只是申報,不會初始化。據我所見,你還沒有分配任何使用=運算符的customFields。 –

回答

-1

是的,我通過固定ngOnInit初始化的對象,它的工作的。 代碼: 接口:

export interface ScanuploadInterface { 
    docVia string;customFields:any; 
    associateDocs :any; 
} 

型號:

import {ScanuploadInterface} from '../../interfaces/scanupload.interface'; 
holeDocument = <ScanuploadInterface>{}; ngOnInit(){this.holeDocument.customFields = {}; 
     this.holeDocument.associateDocs = {};} 

HTML:* ngFor動態輸入字段

<input id="{{list.fieldName}}" name="{{list.fieldName}}" type="{{list.fieldType}}" class="form-control" [(ngModel)]="holeDocument.customFields[list.fieldName]" > 

感謝您的回覆傢伙。