值没有传递给angularjs中的视图

时间:2017-05-19 00:36:42

标签: angularjs node.js

我试图在angularJS中显示一个值,但无法弄清楚它为什么不能打印。

我已将变量定义为$scope.totalBTC,并使用{{totalBTC}}调用它。似乎直截了当但我无法使其发挥作用。

app.js



app.controller('exchangeValuesController', function($scope, $http) {
  $scope.totalBTC = 0;
  $scope.exchangeValues = [];

  var request = $http.get('/exchange-values');

  request.success(function(exchangeValues) {

    $scope.exchangeValues = exchangeValues;
    $scope.totalBTC = exchangeValues.totalBTC;
    console.log('totalBTC: ' + exchangeValues.totalBTC) // totalBTC: 9.45
  });

  request.error(function(err) {
    console.log('Error: ' + err);
  });
});

index.ejs

<h1>Poloniex Balances</h1>
<div ng-controller="exchangeValuesController">
  <p>
    <!-- totalBTC will not print on the ejs page -->
    <h1>Total BTC Value: {{totalBTC}}</h1>
  </p>
  <table class="table">
    <tr>
      <th>Currency</th>
      <th>Amount</th>
      <th>rate</th>
      <th>Total BTC Value</th>
    </tr>
    <tr ng-repeat="currency in exchangeValues">
      <!-- these values are correctly printing  -->
      <td>{{currency.currency}}</td>
      <td>{{currency.amount}}</td>
      <td>{{currency.rate}}</td>
      <td>{{currency.totalBTC}}</td>
    </tr>
  </table>
</div>
&#13;
&#13;
&#13;

如何正确显示{{totalBTC}}?

1 个答案:

答案 0 :(得分:0)

看起来服务器返回为数组,因此请尝试使用

$scope.totalBTC = exchangeValues[0].totalBTC;