2017-08-11 94 views
0

我是新来的角。提交之后,我在查看表单字段时查了很多答案,但似乎没有任何结果。 这里是我的HTML代码:无法在角度提交后清除表单域

<form name="myForm" ng-submit="formSubmit()" class="form-horizontal"> 

     <div class="form-group"> 

     <input type="text" class="form-control" ng-model="user.FullName" placeholder="Full Name" required=""/> 

     </div> 
     <div class="form-group"> 
     <input type="text" class="form-control" ng-model="user.Address" placeholder="Address" required=""/> 
     </div> 
    <button type="submit" class="btn btn-primary" style="background-color: purple;">Submit</button> 

</form> 

这里是我的JS代码:

var myApp = angular.module('myApp', ['ui.router']); 
myApp.controller("RegisterCtrl", function ($window,$scope,$http) { 
    $scope.user={} 
    $scope.formSubmit=function(){ 
    $http({ 
     method:'POST', 
     url:'myurl', 
     data:$scope.user, 
     headers:{'Content-Type':'application/json'} 
    }).then(function(res){ 
      console.log(res); 
      $scope.myForm.$setPristine(); 
      $scope.myForm.$setPristine(true); 
      $scope.myForm='';  


     }) 
    } 
    }); 

我试图setPristine以及setUntouched但没有工作。

+0

这个问题是不正确的标记。这个论坛是为角度2+。请将标签更改为angularjs – RRForUI

+0

@RRForUI这不是一个论坛,您可以建议修改来更改标签。 – jonrsharpe

回答

1

我没有在代码中看到奇怪的东西,我根据你的代码制作了plnkr,我做的修改如下。也把plnkr sample

控制器

var myApp = angular.module('myApp', ['ui.router']); 
myApp.controller("RegisterCtrl", function ($window,$scope,$http) { 
    $scope.user={} 
    $scope.formSubmit=function(){ 
    $http({ 
     method:'POST', 
     url:'myurl', 
     data:$scope.user, 
     headers:{'Content-Type':'application/json'} 
    }).then(function(res){ 
     $scope.myForm.$setPristine(); 
     $scope.user = {}; 
     }, function(rej){ //error}); 
} 
}); 
+0

可能不适合你,因为没有进入promise的succes状态,试着把'$ scope.myForm。$ setPristine();'也放在rejec中,像我的plnkr那样。 –

1

你应该尝试

var myApp = angular.module('myApp', ['ui.router']); 
myApp.controller("RegisterCtrl", function ($window,$scope,$http) { 
    $scope.user={} 
    $scope.formSubmit=function(){ 
    $http({ 
     method:'POST', 
     url:'myurl', 
     data:$scope.user, 
     headers:{'Content-Type':'application/json'} 
    }).then(function(res){ 
     $scope.$broadcast('show-errors-reset'); 
     $scope.forms.user = {}; 
     $scope.forms.userFrom.$setPristine = true; 
     }, function(rej){ //error}); 
} 
});