当我提交表单时,输入文本正在清除,但上传的文件没有清除。我使用了$ 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>
答案 0 :(得分:0)
通过清除表单的DOM,表单将被清除。
的document.getElementById(&#34; MyformId&#34)。复位();
如果有另一种方法告诉