简体   繁体   English

Angular2 rxjs数据异步

[英]Angular2 rxjs data async

I make this template for display computed something. 我使此显示模板计算了一些内容。

compute function is getPredictedRate(). 计算函数是getPredictedRate()。

  <div class="ranking-list-container panel panel-default" *ngIf="isTasteGameRanking">
    <ul class="ranking-list list-group">
      <li class="ranking-element list-group-item row" *ngFor="let gameRate of gameRates | slice:0:10; let i = index;">
        <div class="index col-xs-1"> {{ i + 1 }} </div>
        <div class="game-title col-xs-10"><a (click)="moveToGameDetailPage(gameRate.gr_title)" > {{ gameRate.gr_title }} </a></div>
        <div class="game-rate col-xs-1" > {{ getPredictedRate(gamRate.gr_title) }} </div>
      </li>
    </ul>
  </div>

This is getPredictedRate() 这是getPredictedRate()

 public getPredictedRate( targetGame:string ) {
    this.currentUser = JSON.parse(localStorage.getItem('currentUser'));
    let num = 5;
    this.gameService.getGameByTitle( targetGame ).subscribe(
      game => {
        this.game = game;
        this.userService.getCompareUsersByTargetUserId( this.currentUser.id, num ).subscribe(
          compareUsers => {
            this.users = compareUsers;
            let result = this.predictedGameRateService.computePredictedGameRate( this.game, this.currentUser, this.users );
            console.log( result );
          }
        );
      }
    );
  }

If i call this function on template. 如果我在模板上调用此函数。 this function was called over and over and over again. 一遍又一遍地调用此函数。

What i miss in this code? 我在此代码中缺少什么? Please Help me. 请帮我。 I can't sleep two days. 我不能睡两天。 Because of this problem... 因为这个问题...

The template re-evaluates the expression repeatedly. 模板反复重新计算表达式。 One solution could be to simply change it to: 一种解决方案是将其简单地更改为:

    <div class="game-rate col-xs-1" >{{result}}</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM