2
this.procFilterResults: any[];  
this.procservices.GetProcData(this.selectedServer, this.selectedProjectName, 
           this.selectedProc, this.procInput,_inputs) 
    .subscribe(res => {     
      this.procFilterResults = JSON.parse(res); 
      this.Keys=Object.keys(this.procFilterResults[0]);     
      console.log(this.procFilterResults); 
     }, 
     error => this.errorMessage = <any>error); 

<table class="table table-bordered"> 
     <thead> 
      <th *ngFor="let key of Keys"> 
      {{key}} 
      </th> 
     </thead> 
    <tbody> 
     <tr *ngFor= 'let res of procFilterResults'> 

     </tr> 
    </tbody> 
</table> 

基于我的输入,我得到每个请求的新对象。我想将结果绑定到表格。 有没有什么方法可以将绑定到HTML表的对象反序列化。使用Angular2将动态对象绑定到Html

回答

1

您可以在二维格式中创建一个考虑重新格式化的动态对象。我没有测试下面的代码,请尝试一次。

// Add this inside subscribe 
this.Keys = (this.procFilterResults.length || []) && 
      Object.keys(this.procFilterResults[0]); 
this.results = this.procFilterResults.map(
    element => { 
    let obj = []; 
    this.Keys.forEach(key => obj.push({key: key, value: element[key]})) 
    return obj; 
    } 
) 

的Html

<table class="table table-bordered"> 
    <thead> 
     <th *ngFor="let key of Keys"> 
      {{key}} 
     </th> 
    </thead> 
    <tbody> 
     <tr *ngFor='let res of results'> 
     <td *ngFor="let item of res"> 
      {{item.value}} 
     </td> 
     </tr> 
    </tbody> 
</table> 

我故意有keyresult阵列,这样在将来你可以很容易地进行搜索,过滤,排序容易样的功能。

+0

这样做后我得到了不同的错误... – Venu

+0

你得到什么错误? –

+0

未处理的承诺拒绝:模板分析错误: 由于它不是'td'的已知属性,因此无法绑定到'ngForIn'。 ( “ {{RES}} ] * ngFor = ”在res让项目“> {{item.value}} ”):ProcComponent @ 83 :22 属性绑定ngForIn未被嵌入模板上的任何指令使用。确保属性名拼写正确,所有指令都列在“@ NgModule.declarations”中。 (“ – Venu