gpt4 book ai didi

javascript - 如何重置 ng-model 值?

转载 作者:行者123 更新时间:2023-11-28 05:38:24 25 4
gpt4 key购买 nike

我在 ng-change 事件中为 html 中的输入数字框编写了代码。当我改变它的值时,事件就会被调用。但我不能更改超过 2 个值。如果我更改模型值将从第一个值重新绑定(bind)。例如:如果我将值从 1 滚动到 2 那么就没有问题。如果我将 2 更改为 3,那么在模型中它会绑定(bind)为 1。但我想要 3 作为模型值。我不知道如何处理这个问题。请帮我。提前致谢。

这是我的 html 代码

<input class="numeric" min="1" type="number"
ng-change="PriceCardItemQtyChanged(this)"
ng-model="priceCardQtyModel"
style="width:50px;" />

这里是PriceCardItemQtyChanged()

priceCardModalScope.PriceCardItemQtyChanged = function(e) {
for (var i = 0; i < priceCardModalScope.priceCard.length; i++) {
if (priceCardModalScope.priceCard[i].ConsultationParamId == e.dataItem.ConsultationParamId && priceCardModalScope.priceCard[i].IsSelected == true) {
priceCardModalScope.priceCard[i].SelectedItemTotalPrice = e.priceCardQtyModel * e.dataItem.ItemPrice;
}
}
calculateTotalAmount();

return true;
}

function calculateTotalAmount() {
$scope.selectedItemPriceList = priceCardModalScope.priceCard.filter(getPriceList);

function getPriceList(e) {
return (e.IsSelected == true);
}
priceCardModalScope.totalPriceAmountModel = 0;
for (var i = 0; i < $scope.selectedItemPriceList.length; i++) {
priceCardModalScope.totalPriceAmountModel = priceCardModalScope.totalPriceAmountModel + $scope.selectedItemPriceList[i].SelectedItemTotalPrice;
}
}

我的错误屏幕如下所示:

enter image description here

最佳答案

angular.module('app', []).controller('MyCtrl', function($scope) {
$scope.initial = [{
data1: 10,
data2: 20
}];
$scope.datas = angular.copy($scope.initial);
$scope.reset = function () {
$scope.datas = angular.copy($scope.initial);
// or
// angular.copy($scope.initial, $scope.datas);
}
});

<div ng-app="app" ng-controller="MyCtrl">
<div ng-repeat="data in datas">
<input type="text" ng-model="data.data1" />
<input type="text" ng-model="data.data2" />
</div>
<a ng-click="reset()">Reset to initial value</a>
or
<hr />
<p ng-repeat="data in datas">{{data.data1}}, {{data.data2}}</p>{{initial}}
</div>

//尝试这是您的要求的示例。

关于javascript - 如何重置 ng-model 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39159288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com