如何在數據forEach()
內撥打this.firms
?Angular2 for each cant read property
我知道如何在Angular1做到這一點,但不與角2
我當前的項目目前它的工作的foreach罰款外,但不是內。
console.log(this.firms[0].name); // works
var a = 0;
console.log("--------------");
data.forEach(function (eachObj) {
console.log("firms check!");
console.log(this.firms); // not working
a = a + eachObj.income;
eachObj.name = this.firms[data.firmid - 1].name; // wont work
});
錯誤:
Cannot read property 'firms' of undefined
請爲我們提供一個https://plnkr.co/edit/ – SjaakvBrabant
[將範圍傳遞給forEach]的可能重複(http://stackoverflow.com/questions/ 19733758 /傳遞範圍到foreach) – Erazihel