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>
如何使形式來明確提交表單 這在輸入類型=「文件」是沒有得到清除