javascript - Angular2 中页面发出过多 HTTP 请求

标签 javascript angular ionic-framework ionic3

这是我的 .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

Call a function inside ngFor in angular2

关于javascript - Angular2 中页面发出过多 HTTP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44575303/

相关文章:

angular - 如何在主渲染过程之外调用 Electron 更新器

Javascript循环对象数组

javascript - 如何在ionic 3+中更改单个页面的背景颜色

javascript - 我的汉堡图标没有显示导航菜单

javascript - 每次我点击谷歌地图 API 中的位置点时,信息窗口都会显示在同一位置

javascript - 使用 file_get_contents() 和 implode() 将数组传递给 javascript 不起作用

javascript - ui.router 没有重新加载 Controller

javascript - 隐藏子组件Vue中的主div

angular - 如何让图片留在容器内不溢出

cordova - 在 ionic cordova 中使用 twitter 登录无法使用 $cordovaOauth.twitter