Javascript 回调函数后未更新Angular2视图
我使用Meteor-Angular2和slingshot包将图像上传到S3存储。从函数返回并分配给绑定字符串时,视图未更新。(setTimout函数正在工作并更新视图,但上载器函数未工作)Javascript 回调函数后未更新Angular2视图,javascript,meteor,angular,meteor-slingshot,Javascript,Meteor,Angular,Meteor Slingshot,我使用Meteor-Angular2和slingshot包将图像上传到S3存储。从函数返回并分配给绑定字符串时,视图未更新。(setTimout函数正在工作并更新视图,但上载器函数未工作) 导出类AdminComponent{ 公共URL变量:string=“ynet.co.il”; 构造函数(){ this.uploader=newslingshot.Upload(“myfileuploades”); 设置超时(()=>{ this.urlVariable=“视图已更新”; }, 10000)
导出类AdminComponent{
公共URL变量:string=“ynet.co.il”;
构造函数(){
this.uploader=newslingshot.Upload(“myfileuploades”);
设置超时(()=>{
this.urlVariable=“视图已更新”;
}, 10000);
}
onFileDrop(文件:file):无效{
log('gotfile2');
this.uploader.send(文件,函数(错误,下载URL){
如果(错误){
//日志服务详细响应
}
否则{
this.urlVariable=“视图未更新”;
}
});
}
}
使用箭头函数(()=>
)而不是函数()
,以保留的范围。
this.uploader.send(file, (error, downloadUrl) => {
if (error) {
// Log service detailed response
}
else {
// now `this.` points to the current class instance
this.urlVariable = "View not updated";
}
});
这一款适合我:(窄功能+Ngzone)
视图仍然没有更新,除非我移动到路由器中的另一个路由或执行某些操作以刷新页面。调用
onFileDrop
的位置在哪里?谢谢!使用您的答案并添加到其中。ngZone.run(()=>{this.urlVariable=“视图未更新”;});它现在正在工作并立即更新!:-)
this.uploader.send(file, (error, downloadUrl) => {
if (error) {
// Log service detailed response
}
else {
// now `this.` points to the current class instance
this._ngZone.run(() => {this.urlVariable = "View not updated"; });
}
});