我正在尝试使用Angular [ng-file-upload [(https://github.com/danialfarid/ng-file-upload)指令实现文件上传。我尽可能地跟踪文档。我希望用户能够填写表单,附加图像,然后点击提交按钮。当发生这种情况时,我想将文件和表单输入字段作为json发送到服务器。
当我运行检查以查看image.$valid
是否为真时,我收到错误``。我不确定我在这里缺少什么。
这是我的控制器:
var app = angular.module('myApp', ['ngAnimate','ui.bootstrap', 'ngFileUpload']);
app.controller('NewPostQuestionCtrl', ['$scope', '$http', 'Upload', function($scope, $http, Upload) {
$scope.image = {};
$scope.form = {};
$scope.postQuestion = {
token: $scope.token,
employer_id: $scope.employer_id,
question: $scope.question,
published: $scope.published
};
$scope.submit = function(postQuestionAttributes, image) {
console.log(postQuestionAttributes.$valid)
console.log(image.$valid)
if (image && image.$valid && !image.$error && postQuestionAttributes.$valid) {
$scope.upload(image, postQuestionAttributes);
}
};
$scope.upload = function(file, postQuestionAttributes) {
Upload.upload({
url: 'cms/posts',
fields: postQuestionAttributes,
file: image
}).progress(function (evt) {
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
console.log('progress: ' + progressPercentage + '% ' + evt.config.file.name);
}).success(function (data, status, headers, config) {
console.log('file ' + config.file.name + 'uploaded. Response: ' + data);
}).error(function (data, status, headers, config) {
console.log('error status: ' + status);
})
};
}]);
这是我的表格:
<form accept-charset="UTF-8" name="form.postQuestionForm" ng-submit="submit(postQuestion, image)" class="new_post_item" novalidate>
<input name="utf8" type="hidden" value="✓">
<input name="authenticity_token" type="hidden" ng-model="postQuestion.token" ng-init="postQuestion.token='<%= form_authenticity_token %>'">
<input name="employer_id" type="hidden" ng-model="postQuestion.employer_id" ng-init="postQuestion.employer_id='<%= current_employer.id %>'">
<div class="form-group">
<label>Question</label>
<textarea class="question-textbox" name="question" ng-model="postQuestion.question" required></textarea>
</div>
<div class="form-group">
<label>Image</label>
<div class="button" ngf-select ng-model="image" name="image" ngf-pattern="image/*" accept="image/*" ngf-max-size="150KB" required>Select</div>
</div>
<div class="form-group">
<label>Publish Question</label>
<select class="" name="published" ng-model="postQuestion.published" required>
<option value="true">Publish</option>
<option value="false">Don't Publish</option>
</select>
</div>
<input class="submit-btn" name="commit" type="submit" value="Publish Post" ng-disabled="form.postQuestion.$invalid">
</form>
答案 0 :(得分:1)
表单对象是map,其中字段是带有验证结果的键,因此要检查name =“image”之类的单个输入是否有效,您必须这样做:
var is_valid = form.postQuestionForm["image"].$valid;
这里是小jsfiddle示例:https://jsfiddle.net/nran9uhh/2/
答案 1 :(得分:1)
$有效用于表单元素而不是文件,因此您可以拥有$scope.form.image.$valid
,但文件本身只有$error
与该单个文件相关。
因此,在提交代码中检查$scope.postQuestionForm.image.$valid
而不是image.$valid
。
但它们似乎都是多余的检查,所以在你的情况下检查表单本身的有效性,这意味着表单中的所有元素都是有效的:
$scope.submit = function(postQuestionAttributes, image) {
if ($scope.postQuestionForm.$valid) {
$scope.upload(image, postQuestionAttributes);
}
};