2017-05-10 161 views
0

我越來越ngFor嵌套問題,它顯示'不能讀取未定義的屬性'字段。Angular 2:ngFor嵌套問題

我試過提及其他問題,但無法得到它。請幫助我如何使數據正確

// service call 
 

 
this.xmldataService.getReport().subscribe(
 
     (report) => { 
 
     this.group = report.CrystalReport.Group; 
 
     }); 
 
     
 
     
 
// Error 
 

 
ERROR TypeError: Cannot read property 'Field' of undefined 
 
    at Object.eval [as updateDirectives] (AppComponent.html:23) 
 
    at Object.debugUpdateDirectives [as updateDirectives] (core.es5.js:12641) 
 
    at checkAndUpdateView (core.es5.js:12053) 
 
    at callViewAction (core.es5.js:12367) 
 
    at execEmbeddedViewsAction (core.es5.js:12339) 
 
    at checkAndUpdateView (core.es5.js:12054) 
 
    at callViewAction (core.es5.js:12367) 
 
    at execComponentViewsAction (core.es5.js:12313) 
 
    at checkAndUpdateView (core.es5.js:12059) 
 
    at callWithDebugContext (core.es5.js:13041)
<table class="table table-bordered" *ngFor="let head of group; let i = index"> 
 
    <thead> 
 
     <tr> 
 
     <th *ngFor="let d of head.GroupHeader.Section.Text; let i = index">{{d.TextValue}}</th> 
 
     </tr> 
 
    </thead> 
 
    <tbody> 
 
     <tr> 
 
     <td *ngFor="let d of head.Details.Section.Field; let i = index">{{d.value}}</td> 
 
     </tr> 
 

 
    </tbody> 
 
    </table>

下面

是我的JSON,我得到錯誤

{ 
 
\t "@Level": "2", 
 
\t "Section": { 
 
\t \t "@SectionNumber": "0", 
 
\t \t "Field": [{ 
 
\t \t \t "@Name": "GroupNameTEAMNO1", 
 
\t \t \t "@FieldName": "GroupName ({PDA112P.TEAM_NO})", 
 
\t \t \t "FormattedValue": "DL04", 
 
\t \t \t "Value": "DL04" 
 
\t \t }, { 
 
\t \t \t "@Name": "ASMPROCNO1", 
 
\t \t \t "@FieldName": "{PDA112P.ASM_PROC_NO}", 
 
\t \t \t "FormattedValue": "3H878", 
 
\t \t \t "Value": "3H878" 
 
\t \t }, { 
 
\t \t \t "@Name": "UNITNO1", 
 
\t \t \t "@FieldName": "{PDA112P.UNIT_NO}", 
 
\t \t \t "FormattedValue": "H83937", 
 
\t \t \t "Value": "H83937" 
 
\t \t }, { 
 
\t \t \t "@Name": "APPROVEDDATE1", 
 
\t \t \t "@FieldName": "{PDA112P.APPROVED_DATE}", 
 
\t \t \t "FormattedValue": null, 
 
\t \t \t "Value": null 
 
\t \t }, { 
 
\t \t \t "@Name": "REJECTEDDATE1", 
 
\t \t \t "@FieldName": "{PDA112P.REJECTED_DATE}", 
 
\t \t \t "FormattedValue": null, 
 
\t \t \t "Value": null 
 
\t \t }, { 
 
\t \t \t "@Name": "MAINTDATE1", 
 
\t \t \t "@FieldName": "{PDA112P.MAINT_DATE}", 
 
\t \t \t "FormattedValue": "04/20/2017", 
 
\t \t \t "Value": "04/20/2017" 
 
\t \t }, { 
 
\t \t \t "@Name": "RESNFORCHANGE1", 
 
\t \t \t "@FieldName": "{PDA112P.RESN_FOR_CHANGE}", 
 
\t \t \t "FormattedValue": "CHANGED GET TO GRASP IN WORKING POINT DETAIL NO PCF REQUIRED", 
 
\t \t \t "Value": "CHANGED GET TO GRASP IN WORKING POINT DETAIL NO PCF REQUIRED" 
 
\t \t }, { 
 
\t \t \t "@Name": "ISSUEDDATE1", 
 
\t \t \t "@FieldName": "{PDA112P.ISSUED_DATE}", 
 
\t \t \t "FormattedValue": null, 
 
\t \t \t "Value": null 
 
\t \t }] 
 
\t } 
 
}

下面

是組標籤

輸出

{"@Level":"1","GroupHeader":{"Section":{"@SectionNumber":"0","Text":[{"@Name":"Text16","TextValue":"Team"},{"@Name":"Text17","TextValue":"Process"},{"@Name":"Text18","TextValue":"Unit"},{"@Name":"Text19","TextValue":"Maint.\n\nDate"},{"@Name":"Text21","TextValue":"Unit Rank /\n\nQuality Issues"},{"@Name":"Text20","TextValue":"Description of Change"},{"@Name":"Text22","TextValue":"Issued \n\nDate"},{"@Name":"Text24","TextValue":"Rejected\n\nDate"},{"@Name":"Text23","TextValue":"Approved\n\nDate"}]}},"Details":{"@Level":"2","Section":{"@SectionNumber":"0","Field":[{"@Name":"GroupNameTEAMNO1","@FieldName":"GroupName ({PDA112P.TEAM_NO})","FormattedValue":"DL04","Value":"DL04"},{"@Name":"ASMPROCNO1","@FieldName":"{PDA112P.ASM_PROC_NO}","FormattedValue":"3H878","Value":"3H878"},{"@Name":"UNITNO1","@FieldName":"{PDA112P.UNIT_NO}","FormattedValue":"H83937","Value":"H83937"},{"@Name":"APPROVEDDATE1","@FieldName":"{PDA112P.APPROVED_DATE}","FormattedValue":null,"Value":null},{"@Name":"REJECTEDDATE1","@FieldName":"{PDA112P.REJECTED_DATE}","FormattedValue":null,"Value":null},{"@Name":"MAINTDATE1","@FieldName":"{PDA112P.MAINT_DATE}","FormattedValue":"04/20/2017","Value":"04/20/2017"},{"@Name":"RESNFORCHANGE1","@FieldName":"{PDA112P.RESN_FOR_CHANGE}","FormattedValue":"CHANGED GET TO GRASP IN WORKING POINT DETAIL NO PCF REQUIRED","Value":"CHANGED GET TO GRASP IN WORKING POINT DETAIL NO PCF REQUIRED"},{"@Name":"ISSUEDDATE1","@FieldName":"{PDA112P.ISSUED_DATE}","FormattedValue":null,"Value":null}]}}}

+0

你可以給你的數據結構/模型的更多細節?例如組?頭? – sugarme

+0

添加了json供您參考 –

+0

如果您輸入「service call」,console.log(this.group)的輸出如何?我的意思是,如果您添加爲: this.xmldataService.getReport()。subscribe( (report)=> { this.group = report.CrystalReport.Group; console.log(this.group); }); – sugarme

回答

0

我想,問題是,在「組」收集的所有項目並非都head.Details.Section場。我建議您記錄所有物品,以確保它們具有適當的字段。

+0

我已添加json供您參考 –

0

你的「價值」是在JSON的模板,但大寫小寫:「值」

通過你的整個JSON看後,你也缺少了「部分」財產上的幾組。看到這個小提琴,超過整個JSON遍歷並輸出各組的「部分」:https://jsfiddle.net/j3eo4u7y/(打開控制檯和命中運行,看日誌)

j.CrystalReport.Group.map(g => g.Details).map(d => console.log(d.Section)) 
+0

仍然會給我無法讀取屬性'Field'的undefined –

+0

您確定這是您的整個json嗎?高層看起來像「細節」是什麼? – Eeks33

+0

json文件太大,所以我已將它添加到此鏈接https://jsonblob.com/a995ae6b-3589-11e7-ae4c-fff100ae78bd –