提交POST AngularJS后表单不清除

时间:2015-07-29 15:16:08

标签: javascript jquery angularjs forms yeoman

我使用angularjs前端和Play框架后端来处理发布的数据。

我面临的挑战是,在我点击提交成功发布数据后,表单没有重置。

我的观点如下,如下所示。

<form name="signupForm" ng-submit="signup()" novalidate>
 <div>
   <label for="email">Email</label>
   <input name="email" class="form-control" type="email" id="email" placeholder="Email"
          ng-model="email">
   </div>
 <div>
   <label for="password">Password</label>
   <input name="password" class="form-control" type="password" id="password"
          placeholder="Password" ng-model="password">

 </div>
 <button type="submit" class="btn btn-primary">Sign up!</button>
</form>

My Angular控制器如下

angular.module('clientApp')
   .controller('SignupCtrl', function ($scope, $http, $log) {
     $scope.signup = function() {
       var payload = {
         email : $scope.email,
         password : $scope.password
       };

       $http.post('app/signup', payload)
           .success(function(data) {
             $log.debug(data);
           });
     };
   });

我正在使用Chrome浏览器。如何在单击提交后清除电子邮件和密码字段?

1 个答案:

答案 0 :(得分:1)

$scope.email$scope.password设置为null,如

$http.post('app/signup', payload)
   .success(function(data) {
     $log.debug(data);

      $scope.email = null;
      $scope.password = null;
      $scope.signupForm.$setPristine(); //Set form to pristine mode
   });