【发布时间】:2017-11-10 17:06:21
【问题描述】:
<form>
Low: <input type="number" name="lowRange"
ng-model="weight" ng-model-options = "{updateOn:'submit'}">
<button type="submit">Assign</button>
<button type="button" ng-click="resetValue()">Discard</button>
</form>
<p>$scope.weight: {{weight}}</p>
weight 与ng-model 绑定。通过使用ng-model-options="{updateOn:'submit'}",ng-model 值仅在单击提交按钮后更新。
当用户修改输入中的值并单击丢弃时,我想将输入框中的值重置为当前的 ng-model 值。
我通过ng-click="resetValue()"来做到这一点
$scope.resetValue = function(){
$scope.weight = $scope.weight; //if +1 here will update ng-model
}
但是当变量相同时赋值不起作用。可能是因为它是通过引用分配的。
输入框中的值没有改变。有没有人知道如何强制输入框上的ng-model 更新值?
这是一个演示:http://plnkr.co/edit/NQTieUmdEbxZeBsjk8Jw?%2F=preview&p=preview
【问题讨论】:
标签: javascript angularjs pass-by-reference ngmodel