如何重置ng-model值?

时间:2016-08-26 06:02:06

标签: javascript html angularjs html5

我在html中输入数字框的ng-change事件中编写了代码。当我改变它的值时,会调用该事件。但我不能改变超过2的价值。如果我更改模型值是从第一个值重新绑定。例如:如果我将值从1滚动到2则没有问题。如果我改变2到3然后在模型中它绑定为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

1 个答案:

答案 0 :(得分:0)

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>

//试试这是你的要求的例子。