这是我的 .ts 代码
userCount(event_id){
this.http.get('http://localhost/getCount.php?event_id='+event_id)
.subscribe(res=>{
this.eventUserCount = res.json();
console.log("User NUM :: ", this.eventUserCount)
return this.eventUserCount
});
}
这是调用上述函数的 HTML 代码:
<div *ngFor="let event of events">
....
<ion-col col-6 center text-center>
<button ion-button>
<div> {{ userCount(event.event_id) }} Users</div>
</button>
</ion-col>
...
</div>
我从服务器收到无限响应(从 console.log 可以看出) 我想为每个列表项发出单独的 http 请求并显示每个事件的计数,而不是这种无限的请求和响应循环。 谁能帮我解决这个问题吗?
最佳答案
"Don't use functions in bindings. your function will be called on every change detection cycle. and angular runs x2 cycles in dev mode"
意味着永远不要使用 {{ Fun() }} 它会降低你的性能和糟糕的做事方式。您在 For 循环内的调用。这非常糟糕。
你可以这样尝试
创建事件子组件并传递事件对象
或
在 *ngFor 渲染之前创建完整的对象并传递给 *ngFor
关于javascript - Angular2 中页面发出过多 HTTP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44575303/