2016-06-21 40 views
0

当我提交表单时,输入文本正在清除,但上传的文件未被清除。我已经使用$ scope.addAnswerForm。$ setPristine();但这不起作用。成功提交表单时输入类型文件未被清除

app.directive('fileModel', ['$parse', 
 
     function($parse) { 
 
     return { 
 
      restrict: 'A', 
 
      link: function(scope, element, attrs) { 
 
      var model = $parse(attrs.fileModel); 
 
      var modelSetter = model.assign; 
 

 
      element.bind('change', function() { 
 
       scope.$apply(function() { 
 
       modelSetter(scope, element[0].files[0]); 
 
       }); 
 
      }); 
 
      } 
 
     }; 
 
     } 
 
    ]); 
 
    app.controller("MyCtrl", function($scope, $http) { 
 
     $scope._answer = { 
 
     text: '', 
 
     comment_pic: '' 
 
     }; 
 

 
     $scope.__answer = JSON.parse(JSON.stringify($scope._answer)); 
 

 
     $scope.addAnswer = function(todo) { 
 
     var data = { 
 
      answer: $scope.__answer.text, 
 
      comment_pic: $scope.__answer.comment_pic, 
 
     }; 
 

 

 
     var fd = new FormData(); 
 
     for (var key in data) { 
 
      fd.append(key, data[key]) 
 
     }; 
 

 
     $http({ 
 
      url: '/posturl' 
 
      data: fd, 
 
      method: "POST", 
 
      headers: { 
 
      'Content-Type': undefined 
 
      } 
 
     }).success(function(data, status) { 
 
      todo.answers.push($scope.__answer); 
 
      $scope._answer.comment_pic = ""; 
 
      $scope.__answer = JSON.parse(JSON.stringify($scope._answer)); 
 
     }); 
 
     }; 
 
    });
<div ng-controller="myCtrl"> 
 
    <form name="addAnswerForm"> 
 
    <input ng-model="__answer.text" /> 
 
    <input type="file" file-model="__answer.comment_pic" /> 
 
    <button type="submit" ng-click="addAnswer(discussion)">Post</button> 
 
    </form> 
 
</div>

如何使形式来明确提交表单 这在输入类型=“文件”是没有得到清除 enter image description here

回答

0

通过清除的DOM文件后窗体的形式越来越清晰。

document.getElementById(“MyformId”)。reset();

如果还有其他方法请告诉