在不带括号的函数内调用函数
Call a function within a function without parentheses
我在 angular 中有一项服务,在某些情况下会动态创建 html 输入,如下所示:
this.fileInput = this.document.createElement('input');
this.fileInput.setAttribute('type', 'file');
this.fileInput.setAttribute('accept', 'image/*');
this.fileInput.addEventListener('change', this.onFileSelected);
this.fileInput.click();
onFileSelected 方法:
async onFileSelected(event) {
const file = event.target.files[0];
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = (_event) => {
this.uploadFile(reader.result);
};
}
当我调用它时出现以下错误:
this.uploadFile is not a function
我猜这是因为我调用的函数没有括号,但我不能用任何其他方式调用它。
有什么解决方法吗?
修改添加处理程序的方式
this.fileInput.addEventListener('change', e=> this.onFileSelected(e));
通常,this
现在应该正确引用您的 class(我相信它会引用文件输入 dom 元素)
我在 angular 中有一项服务,在某些情况下会动态创建 html 输入,如下所示:
this.fileInput = this.document.createElement('input');
this.fileInput.setAttribute('type', 'file');
this.fileInput.setAttribute('accept', 'image/*');
this.fileInput.addEventListener('change', this.onFileSelected);
this.fileInput.click();
onFileSelected 方法:
async onFileSelected(event) {
const file = event.target.files[0];
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = (_event) => {
this.uploadFile(reader.result);
};
}
当我调用它时出现以下错误:
this.uploadFile is not a function
我猜这是因为我调用的函数没有括号,但我不能用任何其他方式调用它。 有什么解决方法吗?
修改添加处理程序的方式
this.fileInput.addEventListener('change', e=> this.onFileSelected(e));
通常,this
现在应该正确引用您的 class(我相信它会引用文件输入 dom 元素)