[英]Updating value in ng-repeat based on dropdown
我正在使用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 。 如果有人遇到類似情況,我將不勝感激有關如何使其正常工作的建議! 謝謝!
請更新您的功能
$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;
})
}
};
$ 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屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.