AngularJS 从 HTML 页面而非 .js 文件调用函数时,$scope 未定义

AngularJS $scope is undefined when a function is called from HTML page not .js file

我有这个:$scope.products;
我这样设置它的值:$scope.products = plans; 其中 plans 是我从电话中得到的结果。 如果我调试,在这里,调试器中有 $scope$scope.products

示例:

但后来我从我的 .html 中执行此操作:

<td>
  <span data-i18n="psngr.billing.pay.overview.total">
    Total
  </span> 
  <label class="bold">
    <span ng-bind-html="prices.currency"></span>
    {{ roundPrice(products[0].price.val)}}
  </label>
  <span id="vat" ng-show="prices.currency_code === 'EUR'"> 
    (+ 
    <span ng-bind-html="prices.currency"></span>
    {{products[0].vat}} 
    <span data-i18n="psngr.billing.vat"></span>
    )
  </span>
</td>

基本上是从我的 .js 文件中调用 roundPrice 方法,它是这样做的:

$scope.roundPrice = function(price) {
  if (!price) {
    return 0;
  }
  // potentially round the price
  if (price % 1 === 0) {
    return price.toFixed(0);
  }
  return price.toFixed(2);
};

但是这里我的范围是未定义的,也是价格原因。为什么?

示例:

查看我们从服务器获得的响应后,我更好地理解了问题所在:我得到的是整数价格。所以当然 price.val 会得到 underfined。这意味着它现在可以工作了,在我将代码更改为:

roundPrice(products[0].price)}}