【问题标题】:How to not update a model until form submission in Angular?如何在 Angular 中提交表单之前不更新模型?
【发布时间】:2015-09-05 15:49:45
【问题描述】:

我希望 Angular 在用户提交表单之前不会更新模型。或者,以另一种方式,仅在用户提交表单时更新模型。

<form ng-submit="process()">
    Value is : {{ model.property }}
    <input type="text" ng-model="model.property">
    <button type="submit">Submit</button>
</form>

我在想ng-model-options="{ updateOn: null }" 之类的东西,但它不起作用。

这是否可能而不会变得太“hacky”(例如在提交时获取每个输入的值)?

【问题讨论】:

标签: javascript angularjs forms binding


【解决方案1】:

您应该使用angular.copy 克隆您的对象。

 $scope.formData = angular.copy($scope.model);

<form ng-submit="process()">
    Value is : {{ formData.property }}
    <input type="text" ng-model="formData.property">
    <button type="submit">Submit</button>
</form>

然后在更新模型的过程中。

 $scope.model = angular.copy($scope.formData);

您将使用临时模型而不是“真实”模型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-16
    • 2016-05-18
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    • 2018-09-04
    • 1970-01-01
    • 2014-09-10
    相关资源
    最近更新 更多