1

我正在尝试使用 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>
4

2 回答 2

1

$valid 用于表单元素而不是文件,因此您可以拥有$scope.form.image.$valid,但文件本身只会$error与该单个文件相关。因此,在您的提交代码中检查$scope.postQuestionForm.image.$valid而不是image.$valid.

但它们似乎都是多余的检查,所以在你的情况下检查表单本身的有效性,这意味着表单中的所有元素都是有效的就足够了:

$scope.submit = function(postQuestionAttributes, image) {
    if ($scope.postQuestionForm.$valid) {
        $scope.upload(image, postQuestionAttributes);
    }
};
于 2015-08-29T18:34:52.863 回答
1

表单对象是 map ,其中字段是带有验证结果的键,因此要检查诸如 name="image" 之类的单个输入是否有效,您必须这样做:

var is_valid = form.postQuestionForm["image"].$valid;

这是一个小的 jsfiddle 示例:https ://jsfiddle.net/nran9uhh/2/

于 2015-08-28T18:42:46.633 回答