如何在 Angular 提交表单之前不更新模型?

How to not update a model until form submission in Angular?

我正在寻找 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);

您将使用临时模型,而不是您的 "real" 模型。