我希望 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”(比如在提交时获取每个输入的值),这是否可能?
最佳答案
您应该使用 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);
您将使用一个临时模型而不是您的“真实”模型。
关于javascript - 如何在 Angular 提交表单之前不更新模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30941917/