2017-08-17 76 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); 
    } 
}