【问题标题】:javascript force update on ng-model value on inputboxjavascript 强制更新输入框上的 ng-model 值
【发布时间】: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>

weightng-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


    【解决方案1】:

    您需要告诉输入字段重置。

    <form name="myForm">
      Low: <input type="number" name="lowRange" ng-model="weight" ng-model-options = "{updateOn:'submit'}">
      <br>
      <button type="submit">Assign</button>
      <button type="button" ng-click="myForm.lowRange.$rollbackViewValue();">Discard</button>
    </form>
    

    【讨论】:

      猜你喜欢
      • 2016-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-19
      • 2017-02-21
      • 1970-01-01
      相关资源
      最近更新 更多