Joh*_*nes 1 typescript angular
基本上标题已经说明了问题。我附上了代码摘录,它应该可以表达我的想法。
export class MyComponent {
constructor(private element: ElementRef, private myservice:MyService){}
onChange(event: any): void {
// works fine
var imageElem = this.element.nativeElement.querySelector('.image');
reader.onloadend = function() {
// works fine
console.log(imageElem)
// does not work
this.doSome(src);
// does not work
this.myservice.doSome();
}
reader.readAsDataURL(file);
}
doSome(src:string) {}
}
Run Code Online (Sandbox Code Playgroud)
我会利用箭头函数来实现onloadend类似的功能:
reader.onloadend = () => {
...
}
Run Code Online (Sandbox Code Playgroud)
这种方式this将是MyComponent内部回调的实例,因为:
箭头函数捕获封闭上下文的 this 值
| 归档时间: |
|
| 查看次数: |
821 次 |
| 最近记录: |