angular - array.find 函数阻止访问父类 'this' 对象
问题描述
我在一个角度组件中有这个功能。一旦我尝试查询“myData.Order3”数组,我就无法从“find”函数中访问“this”。'this' 报告为未定义,而在父函数中对 'this' 的其他引用很好。
这显然是一个范围界定问题,但在修复它时遇到了问题。
public toggle():void {
this.freq.toggle();
this.checkState().subscribe(response => {
console.log(this.freq.frequency);
this.myData = response as ArrayCast;
if (this.myData.Order3.find(function(ele) {
return ele == this.freq.frequency; // <= "this" reports as undefined
})){
this.toggleStyle="ui-button-danger";
}
console.log(this.myData);
});
}
解决方案
使用 Lambda 表达式=>
而不是function
if (this.myData.Order3.find((ele) => ele === this.freq.frequency))
推荐阅读
- regex - 如何解析文本块并根据该块中的另一个值获取一个值
- database - CouchDB 永久认证密钥
- python - 返回一个值或将一个值分配给一个变量,然后返回 - 最佳实践?
- typescript - 为什么 Typescript 告诉我 X 型是与 ITSELF 不同的子类型?
- github - Github 渲染 Asciidoc 标注:缺少图标
- angular - Ionic Angular 路由器组件缓存问题
- sql-server - SQL 中此游标的替代基于集合的选项?
- php - php -v 无法识别 Ubuntu 将 PHP 版本更新到 7.4
- python - 烧瓶从无线电输入返回错误值
- postgresql - 有没有办法将postgres db复制到heroku?