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

标签 javascript angularjs forms binding

我希望 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/

相关文章:

javascript - 从 JSON 响应创建一个可观察的敲除数组

javascript - 如何在 Javascript 中创建这种对象

angularjs - WP Rest API + AngularJS : How to grab Featured Image for display on page?

angularjs - 如何在 Angular 谷歌地图中获取center_changed纬度和经度

html - 如何使用html在一行中制作所有输出?

javascript - 不同的组成元素

javascript - 无法在堆栈中存储值

javascript - 如何以 Angular 动态添加新行

jquery - 为什么 jQuery 的 .submit() 不起作用?

javascript - 如何读取表单中的输入数据?