提交表单后如何重置表单和验证(AngularJS)

Tel*_*ary 3 javascript validation angularjs

提交表单后,我想重置表单和所有验证消息。这是小矮人:http ://plnkr.co/edit/992RP8gemIjgc3KxzLvQ? p =preview

我的代码如下:

控制器:

app.controller('MainCtrl', function($scope) {
  $scope.data={fullName:''};

  $scope.submit=function(){
    console.log('submit')
    $scope.myForm.$setPristine();
    $scope.myForm.$setUntouched();
    $scope.data={fullName:''};
  }
});
Run Code Online (Sandbox Code Playgroud)

HTML:

<body ng-controller="MainCtrl">
<form name="myForm" novalidate>
<div>
    <label class="control-label">Name</label>
    <input
            name="full_name"
            type="text"
            ng-model="data.fullName"
            ng-required="true">

    <p ng-if="myForm.$submitted && myForm.$invalid">Validation message</p>
</div>
<button ng-click="submit()" type="submit">
    Submit
</button>
</form>
</body>
Run Code Online (Sandbox Code Playgroud)

我的问题是:当用户在输入中输入名称并单击Submit按钮时,不应显示验证消息,因为应该重置模型,表单和验证。我该怎么办?

我尝试使用$ setPristine()和$ setUntouched(),但它对我不起作用。

在AngularJS中可以做吗?谢谢!

cha*_*tfl 5

我很惊讶,但$setpristine()没有更新$submitted

这可能不是最漂亮的解决方案,但似乎可行:

app.controller('MainCtrl', function($scope, $timeout) {
  $scope.data = {
    fullName: ''
  };
  $scope.submit = function() {
    console.log('submit')
     $scope.data = {
      fullName: ''
    };
    $timeout(function() {
      $scope.myForm.$setPristine();
      $scope.myForm.$setUntouched();
      $scope.myForm.$submitted = false;
    });   
  }
});
Run Code Online (Sandbox Code Playgroud)

DEMO