4
我試圖從DB中檢索數據並將其設置爲作用域2中的作用域變量。我不知道爲什麼。我試過超時區域。氣東西,承諾事情。我真的不知道他們是什麼,所以我無法使用它..請幫助我弄清楚這一點。Angular2中的回調函數中的作用域變量的訪問值
//in service.ts
listFriends(callback) {
result_data = [];
db.transaction(function(tx) {
tx.executeSql('SELECT * from mytable', [], function(tx, results) {
length = results.rows.length;
for (i = 0; i < length; i++) {
//console.log(results.rows.item(i));
result_data.push(results.rows.item(i))
}
callback(result_data);
}, null);
});
}
//in component.ts
public allmyFriends: any;
public self = this;
public test;
constructor(myFriendService: MyFriendService) {
this.myFriendService = myFriendService;
this.myFriendService.listFriends((response) => {
//trying to set value to the scope variable.but not working
this.test="test working";
//same as above
this.allmyFriends = response;
//getting the response from the service successfully
console.log("in list" + this.allmyFriends);
});
}
OMG! NgZone。 run()像魅力一樣工作。非常感謝你@thierry。我用過這個。 _ngZone.runOutsideAngular(),這就是爲什麼它不起作用。你知道嗎? – Nishanthd
酷!很高興聽到;-)'runOutsideAngular'方法用於在觸發Angular2變化檢測的區域之外的Angular2之外執行一些處理。所以這種行爲似乎很正常...... –