2017-08-17 77 views
0

我怎样才能在这个表角/打字稿值..角/打字稿:在表

My console log

我怎样才能获得的代码和标签的价值获得值.. 我试着要做到这一点代码,但它不是工作:

MyComponent.ts

jsonsTypes = []; // define here global variable 
    saveFolder() { 
     let tab = []; 
     this.adminService.getLabelByCode(code).subscribe(
      labelByCode => { 
      tab.push({ 
       'code': code, 
       'label': labelByCode.label 
      }); 
      }); 
      this.jsonTypes.push(tab); 
      //here when I do console.log(tab); ==> I have the result of the picture 
     for (var index = 0; index < this.jsonTypes.length; index++) { 
      console.log(jsonTypes[index]); 
      console.log(jsonTypes[index].code); // ==> undefined    
      // How can I get value ? 
     } 
    } 
+2

做内部订阅,因为值被赋值一次getLabelByCode(代码)完成。你在做这个for循环,因此它通过一个空数组循环。 – trungk18

+0

你的订阅方法并不适合那里。订阅应该完成一次。 – Christian

+0

我在我的第一篇文章中犯了错误 - 我编辑它 – user1814879

回答

0

这将导致代码:

console.log(jsonTypes[index][0].code); 

但问题是,你不需要制作一个“选项卡”的数组。

试试这个......没有测试,但应该工作。

jsonsTypes = []; // define here global variable 
saveFolder() { 
    this.adminService.getLabelByCode(code).subscribe(
    labelByCode => { 
    this.jsonTypes.push({ 
     'code': code, 
     'label': labelByCode.label 
     }); 
    }); 

    for (var index = 0; index < this.jsonTypes.length; index++) { 
    console.log(jsonTypes[index]); 
    console.log(jsonTypes[index].code); 
    } 
}