AngularJs提交表单并重置$原始状态 [英] AngularJs submit form and reset $pristine state

查看:91
本文介绍了AngularJs提交表单并重置$原始状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以该表格为例 http://plnkr.co/edit/fHEBw6dDdG3IVgnmCLb7?p=preview
按下SAVE DRAFT按钮后,如何将表单的$pristine状态设置为true?

Taken this form as example http://plnkr.co/edit/fHEBw6dDdG3IVgnmCLb7?p=preview
How can I put the $pristine state of the form to true after the SAVE DRAFT button is pressed?

推荐答案

您可以调用 form上的$setPristine : http://plnkr.co/edit/wXaFXtuhNH6d4SP2uArm?p=preview

<button ng-click="reset(); form.$setPristine()">RESET</button>
<button ng-click="update(user); form.$setPristine()">SAVE</button>

或者您可以在控制器中调用该方法(确保存在表单之后):

Or you can call the method in your controller (after ensuring that the form exists):

  $scope.update = function(user) {
    $scope.master= angular.copy(user);
    if ($scope.form) $scope.form.$setPristine();
  };

  $scope.reset = function() {
    $scope.user = angular.copy($scope.master);
    if ($scope.form) $scope.form.$setPristine();
  };

演示: http://plnkr.co/edit/Mau7uuDfPlzcn418OdWh?p=预览

这篇关于AngularJs提交表单并重置$原始状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆