我有以下代码
setTimeout(()=>{ console.log("TIMED OUT DATA", this.fetchedData['__zone_symbol__value']) },3000);
我希望在setTimeout之外获得this.fetchedData['__zone_symbol__value']
的值。它甚至可能吗?
答案 0 :(得分:1)
只需在外面宣布并直接引用它。
赞here, in this working example。
export class App {
myData = false;
constructor() {
this.restart();
}
restart(){
console.log("Changind data to false outside timeout");
this.myData = false;
setTimeout(()=>{
console.log("TIMED OUT DATA", this.myData)
this.myData = true;
console.log("TIMED OUT DATA changed", this.myData)
},3000);
}
}