0

我正在使用 ng-repeat 构建一个表格来显示一些信息。显示的列之一是“重量”列。我们将所有以千克为单位的重量存储在数据库中,但需要为用户提供以磅为单位显示重量的选项。

我有一个下拉列表,用户可以在其中选择重量单位,并且在 ng-change 上我正在尝试更新表格。但是,我无法让它工作。

这是我的更改功能(JSFiddle 中的完整示例):

 $scope.ConvertWeights = function () {
        if ($scope.schedWeight.ID == "I") {
            $scope.items.Weight = $scope.items.Weight * 2.2046;
        } else {
            $scope.items.Weight = $scope.items.Weight / 2.2046;
        }

    }

这是我目前正在尝试的JSFiddle 。如果有人遇到过类似情况,我将不胜感激有关如何使其正常工作的任何建议!谢谢!

4

2 回答 2

1

请更新您的功能

$scope.ConvertWeights = function () {
    if ($scope.schedWeight.ID == "I") {
        angular.forEach($scope.items, function(item){
            item.Weight = item.Weight * 2.2046;
        })
    } else {
        angular.forEach($scope.items, function(item){
            item.Weight = item.Weight / 2.2046;
        })
    }
};
于 2014-09-15T12:38:36.293 回答
0

$scope.items 是您的项目集合(一个数组)。你可能试图写:

$scope.ConvertWeights = function () {
    if ($scope.schedWeight.ID == "I") {
        for (var i in $scope.items)
            $scope.items[i].Weight = $scope.items[i].Weight * 2.2046;
    } else {
        for (var i in $scope.items)
            $scope.items[i].Weight = $scope.items[i].Weight / 2.2046;
    }
}

您需要修改每个元素的 Weight 属性,而不是集合本身。

于 2014-09-15T12:36:57.397 回答