角度2 - 绑定加载变量不准确

时间:2017-04-03 01:58:06

标签: javascript angular typescript

我有三个变量(之前),(当前)和(之后)。

我不断地从API获取数据(每1秒)。

我在更新前将当前的API信息保存为(之前),然后在更新后将更新存储在(之后)中。

我正在检查以前是否与(After)不同,如果是,我正在向DOM添加一个类。

现在问题

当我在console.log中显示值时,我可以清楚地看到在某些点上两个值不同。这很好,但是当我将完全相同的数据绑定到DOM时,它们始终相同。有没有人见过这个?

DOM

<div class="ticker" *ngFor="let coinresult of coinResults; let beforecoinresult of beforeCoinResults; let aftercoinresult of afterCoinResults; let i = index;">
    <div class="wrapper" *ngIf="coinresult.name != step2Selection">
        <h1>Before: {{beforecoinresult.amount}} - After: {{aftercoinresult.amount}}</h1>
        <div class="pin" (click)="pinTitle(coinresult.amount, coinresult.name)">
            <i class="material-icons" *ngIf="pinnedCoinAmount != coinresult.amount">gps_not_fixed</i>
            <i class="material-icons selectedCoin" *ngIf="pinnedCoinAmount === coinresult.amount">gps_fixed</i>
        </div>

        <div class="amount" [ngClass]="{amountpinned: pinnedCoinAmount === coinresult.amount, 
                                        amountincrease: beforecoinresult.amount < aftercoinresult.amount,
                                        amountdecrease: beforecoinresult.amount > aftercoinresult.amount}">
                                        {{coinresult.amount}}
        </div>
        <div class="name" [ngClass]="{  namepinned: pinnedCoinAmount === coinresult.amount, 
                                        nameincrease: beforecoinresult.amount < aftercoinresult.amount,
                                        namedecrease: beforecoinresult.amount > aftercoinresult.amount}">
                                        {{coinresult.name}}

        </div>
    </div>

组件

convert(){
      this.beforeCoinResults = this.coinResults;
      if(this.holdings){
      console.log("before " + this.beforeCoinResults[0].amount);
    }
      this.coinResults = [];

      if(this.cryptoSelected && this.step2Selection){
        //convert all the crypto to currencies
        for (var i = 0; i<= this.currencies.length -1 ; i++){
          var tempName = this.currencies[i] as string;
          this.coinResults.push({
            name: this.convertName(tempName as string),
            amount: Math.round(this.holdings * this.ticker[this.convertName(this.step2Selection)].last * this.ticker['USDT_BTC'].last* this.currencyExchange[tempName]*100)/100}
          ); // push
        } // for

        //convert all the crypto to crypto
        for(var i = 0 ; i <= this.coins.length - 1; i++){
          var tempName = this.coins[i] as string;
          this.coinResults.push({
            name: this.convertName(tempName as string), 
            amount: Math.round(this.holdings * this.ticker[this.convertName(this.step2Selection)].last / this.ticker[tempName].last*100000000)/100000000
           }) // push   
        } // for
      } // if cryptoselected

      if(this.regSelected){
            //convert currency to currency
            for (var i = 0; i<= this.currencies.length -1 ; i++){
            var tempName = this.currencies[i] as string;
            this.coinResults.push({
              name: this.convertName(tempName as string),
              amount: Math.round(this.holdings / this.currencyExchange[this.convertName(this.step2Selection)] * this.currencyExchange[tempName]*100)/100
              }) // push
             } // for

             //convert currency to crypto
            for(var i = 0 ; i <= this.coins.length - 1; i++){
              var tempName = this.coins[i] as string;
              this.coinResults.push({
                name: this.convertName(tempName as string), 
                amount: Math.round(this.holdings / this.currencyExchange[this.convertName(this.step2Selection)] / this.ticker['USDT_BTC'].last / this.ticker[tempName].last*100000000)/100000000
                });  //push 
        } // for
      }// if 
      this.afterCoinResults = this.coinResults;
       if(this.holdings){
      console.log("after " + this.afterCoinResults[0].amount);
    }
    }// end convert

0 个答案:

没有答案