javascript - 来自模板的函数调用以无限循环结束 - Angular 6
问题描述
我正在从模板调用一个函数,该函数进行async
调用并在 html 中显示返回的响应。但是,此调用以无限循环结束。
我已经阅读了我们应该使用变更检测策略的解决方案,但这也无济于事。
示例.html:
<div *ngFor = "let data of hugeData; let i = index">
<span *ngIf="getData(i) | async as variable" [innerHTML] = "variable">
</div>
sample.component.ts:
@Component({
selector : 'sample',
templateUrl : './sample.html',
changeDetection : ChangeDetectionStrategy.OnPush
})
sample.component.ts 中的函数调用:
getData(i){
return new Promise (function (resolve,reject){
this.makeApiCall(i).subscribe(response=>{
this.cd.detectChanges();
resolve(response);
})
})
}
makeApiCall(i)
是另一个进行 API 调用并将响应作为 Observable 返回的函数调用。
在这种情况下,它会引发错误:无法读取未定义的属性“makeApiCall”。
请帮我解决这个问题。提前致谢。
解决方案
这this
是引用在Promise
参数中传递的函数,您可能想要this
引用该类
一个简单的解决方案是在Promise
.
前任:
return new Promise ((resolve,reject) => { ...
推荐阅读
- c++ - 已解决:如何避免在 C++ 中创建临时向量?
- open-policy-agent - 为什么 OPA/Rego 中的“违反”和“拒绝”之间有确切的区别?
- python - 每次单击链接时,如何将数据库行增加 1?
- sql - 如何在 BigQuery 中使用空间连接进行 OUTER 连接,以便能够在没有交集的情况下获得 0 的计数值?
- vue.js - 如何测试在 Vue 组件挂载函数中调用 Vuex 模块动作?
- css - 使用 css 如何与不同屏幕尺寸的列表保持一致的距离?
- sql-server - MS Access 子表单组合框在链接到 SQL Server 表时消失
- r - 如何使用 for 循环自动进行网页抓取
- reactjs - 如何将 react-albus 与 Reach Router 一起使用
- sql - 如何从 sql 游标中分离两个值?