javascript - 无法在 setTimeout 函数下获取组件变量
问题描述
我是 Angular(6) 的新手,我遇到了这个问题,我无法在 setTimeout 函数下获取组件变量。看看代码,在那里解释我的问题。
export class ConSellerDraftsolSecOneComponent implements OnInit {
draftSolutionForm: FormGroup;
//Other code
//ngOnIt
ngOnInit() {
this.draftSolutionForm = this.formBuilder.group({
title: ['', Validators.compose([Validators.required, Validators.minLength(6)])],
product: [''],
})
}
//function
autosavedraftsolution() {
//here, it is working
console.log(this.draftSolutionForm);
setTimeout(function () {
//here, it is not working, showing "undefined" and even intellisense is not supporting me here to do this
console.log(this.draftSolutionForm);
}, 3000);
}
}
帮我找出原因。谢谢你。
解决方案
您需要使用 lambda 表达式:
setTimeout(() => {
console.log(this.draftSolutionForm);
}, 3000);
推荐阅读
- monetdb - MonetDB 是否有任何 JDBC 客户端 SSL/TLS 支持?
- c# - c#如何反转字符串,然后以相同的方法撤消反转
- google-sheets - 从宽表格式到长表格式(动态)
- python - 如何编写代码以使用 tifffile 库读取 TIFF 文件并将它们转换为 JPEG?
- opengl - 如何加快大量 VAO 的渲染速度
- r - 交织数据框行的整洁功能方式
- python - 将numpy数组从(1000,1000,3)重塑为(3,1000,1000)并返回
- c# - 禁用单个 .NET Core API 操作的模型验证
- security - 跳转到函数内部的地址
- java - JRE 1.7 可以运行 1.8 个 jars 吗?