[英]How to not update a model until form submission in Angular?
I'm looking for Angular to not update the model until the user submits the form . 我希望Angular在用户提交表单之前不更新模型 。 Or, in another way, to update the model only when the user has submitted the form.
或者,以另一种方式, 仅在用户提交表单后才更新模型。
<form ng-submit="process()">
Value is : {{ model.property }}
<input type="text" ng-model="model.property">
<button type="submit">Submit</button>
</form>
I was thinking of something like ng-model-options="{ updateOn: null }"
but it doesn't work. 我在想类似
ng-model-options="{ updateOn: null }"
但是它不起作用。
Is this even possible without it getting too "hacky" (like getting each input's value on submit)? 如果没有变得过于“ hacky”(例如在提交时获取每个输入的值),这是否有可能?
You should clone your object using angular.copy
. 您应该使用
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>
Then on process you update the model. 然后在处理中,您将更新模型。
$scope.model = angular.copy($scope.formData);
You will be using a temporary model instead of your "real" one. 您将使用临时模型,而不是“真实”模型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.