2017-06-13 76 views
0

迭代中* ngFor對象我有一個數組中subscribethis.Term,如果我安慰,它看起來像問題與angular2

[Discount Condition:Array(3), Stay Consition:Array(2)] 
Discount Condition:Array(3) 
0:object 
    EventType:"success" 
    Heading:"Discount Condition" 
    Help:"Terms" 
1:object 
    EventType:"success" 
    Heading:"Discount Condition" 
    Help:"Terms" 
2:object 
    0:object 
    EventType:"success" 
    Heading:"Discount Condition" 
    Help:"Terms" 

Stay Condition:Array(2) 
0:object 
1:object 

我想打印在組件的HTML一樣,

<div *ngFor="let item of Term"> 
    {{item.Heading}} 
</div> 

如果我檢查檢查使用的元素,它說

<!--template bindings={ 
    "ng-reflect-ng-for-of": "" 
}--> 

這裏設定的功能離子

getTerms(){ 
    this.quoteService.getTerms() 
    .subscribe(
     terms => { 
     this.terms = terms.resultData.Terms; 
     this.notes = terms.resultData.ImpNotes; 
     this.exclusions = terms.resultData.Exclusions; 
     var arr=[]; 
     var strHeading; 
     for(let i=0;i<this.terms.length;i++) { 
      strHeading=this.terms[i].Heading; 
      if (arr.indexOf(strHeading) >= 0) { 
      this.uniqTerm[strHeading].push(this.terms[i]); 
      } 
      else { 
      if (!this.uniqTerm[strHeading]) 
       this.uniqTerm[strHeading] = []; 
       this.uniqTerm[strHeading].push(this.terms[i]); 
       this.Term.push(this.uniqTerm); 
       arr.push(strHeading); 
      }   
     }   
     console.log(this.Term); 
     }, 
     response => { 
     if (response.status == 404) { 
      //this.router.navigate(['NotFound']); 
     } 
     }); 
    } 

任何人都可以請指出我哪裏錯了?

+1

這些信息不夠。請提供更多代碼。 –

+0

@GünterZöchbauer代碼更新的問題,請檢查 –

回答

1

this.Term看起來是一組數組。

<div *ngFor="let item of Term[0]"> 
    {{item.Heading}} 
</div> 

將是一個骯髒的檢查,看看你的數據是否正確。

然後,如果你想迭代這些數組;

<div *ngFor="let item of Term"> 
    <div *ngFor="let elem of item"> 
      {{elem?.Heading}} 
    </div> 
</div> 

可能工作。

+0

都嘗試過,他們都不工作 –

+0

@NikhilRadadiya你的對象在數組裏有一個'Heading'屬性嗎?你可以包含來自這些對象的'console.log'嗎? – echonax

+0

@NikhilRadadiya我很難理解你的數據結構:-)是'保持Consision'和'Discount Condition'數組或對象嗎? – echonax