Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 强制ng repeat在数据绑定中重新运行函数_Javascript_Angularjs_Angularjs Scope_Angularjs Ng Repeat - Fatal编程技术网

Javascript 强制ng repeat在数据绑定中重新运行函数

Javascript 强制ng repeat在数据绑定中重新运行函数,javascript,angularjs,angularjs-scope,angularjs-ng-repeat,Javascript,Angularjs,Angularjs Scope,Angularjs Ng Repeat,我在一个表中使用ng repeat来显示项目及其价格的列表 价格绑定到我的控制器中定义的itemPrice(item) 该函数根据$scope.orderType计算价格 orderType绑定到HTML中的select 更改订单类型时强制更新所有价格的最佳方法是什么? HTML 我正在工作,我没有时间去做一个验证: 我建议不要使用函数(“itemPrice”)。事先进行计算,并将值放入项目结构中的变量中 当其他类型更改时(使用ng change或$scope.$watch..),则重新计算

我在一个表中使用
ng repeat
来显示项目及其价格的列表

  • 价格绑定到我的控制器中定义的
    itemPrice(item)
  • 该函数根据
    $scope.orderType
    计算价格
  • orderType
    绑定到HTML中的
    select
更改订单类型时强制更新所有价格的最佳方法是什么?

HTML
我正在工作,我没有时间去做一个验证:

我建议不要使用函数(“itemPrice”)。事先进行计算,并将值放入项目结构中的变量中

当其他类型更改时(使用ng change或$scope.$watch..),则重新计算并更新项目结构中的变量

比如:

.controller('OrderCtrl', ['$scope', '$http', function($scope, $http) {

    // Set up order types
    $scope.orderTypes = ['Buy Back', 'Sale'];
    $scope.orderType = $scope.orderTypes[1];

    var itemPrice = function(item) {
        if ($scope.orderType === 0) {
            return item.price_buy_back;
        } else if (item.is_used) {
            return item.price_used;
        } else {
            return item.price_new;
        }
    }

    var setItemPrices = function(){

        for(var i = 0; i < $scope.items.length; i++)
        {
            $scope.items[i].itemPrice = itemPrice($scope.items[i]);
        }
    }

    $scope.$watch("orderType", function(newVal, oldVal){
           //update itemPrices
           .... setItemPrices();
    });
    // ...

}]);
.controller('OrderCtrl',['$scope','$http',函数($scope,$http){
//设置订单类型
$scope.orderTypes=[“回购”、“出售”];
$scope.orderType=$scope.orderType[1];
var itemPrice=功能(项目){
如果($scope.orderType==0){
退货项目。价格\回购;
}else if(是否使用项目){
返回项目。使用的价格;
}否则{
返回项目。价格\新;
}
}
var setItemPrices=函数(){
对于(变量i=0;i<$scope.items.length;i++)
{
$scope.items[i].itemPrice=itemPrice($scope.items[i]);
}
}
$scope.$watch(“订单类型”,函数(newVal,oldVal){
//更新项目价格
..设置项目价格();
});
// ...
}]);

嗯,我觉得自己很傻。我原以为我可以把问题归结为Angular的新手,但错误在于简单的旧JavaScript

itemPrice
函数的第一行将
$scope.orderType
与整数进行比较。但是,
$scope.orderType
在函数前面被设置为字符串(
$scope.orderType=$scope.orderType[0]


现在比较已经固定,
ng repeat
会按预期更新,只要
$scope.orderType
发生更改,就调用
itemPrice

ng change
事件附加到您的选择,这将运行$digest循环,并且
ng repeat
将重新绑定到
项目
(尽管它应该已经在更新,因为您有一个
模型绑定到它了…)我添加了一个
ng change
,它调用一个函数将
$scope.orderType
记录到控制台中。当值发生变化时,它确实会记录正确的订单类型,但价格仍然不会更新。我认为这是设置模型的最佳方式。我正在尝试将其拆分为一个具有计算价格属性的模型类,以便简化代码,但我对Angular还是很陌生,所以你帮了我很大的忙。谢谢!
.controller('OrderCtrl', ['$scope', '$http', function($scope, $http) {
    
    // Set up order types
    $scope.orderTypes = ['Buy Back', 'Sale'];
    $scope.orderType = $scope.orderTypes[1];

    // ...
    
    // Get the price of an item
    $scope.itemPrice = function(item) {
        if ($scope.orderType === 0) {
            return item.price_buy_back;
        } else if (item.is_used) {
            return item.price_used;
        } else {
            return item.price_new;
        }
    }
    
    // ...
    
}]);
.controller('OrderCtrl', ['$scope', '$http', function($scope, $http) {

    // Set up order types
    $scope.orderTypes = ['Buy Back', 'Sale'];
    $scope.orderType = $scope.orderTypes[1];

    var itemPrice = function(item) {
        if ($scope.orderType === 0) {
            return item.price_buy_back;
        } else if (item.is_used) {
            return item.price_used;
        } else {
            return item.price_new;
        }
    }

    var setItemPrices = function(){

        for(var i = 0; i < $scope.items.length; i++)
        {
            $scope.items[i].itemPrice = itemPrice($scope.items[i]);
        }
    }

    $scope.$watch("orderType", function(newVal, oldVal){
           //update itemPrices
           .... setItemPrices();
    });
    // ...

}]);