ng-file-upload file.$valid 未定义

ng-file-upload file.$valid is undefined

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

表单对象是地图,其中字段是具有验证结果的键,因此要检查像 name="image" 这样的个人输入是否有效,您必须这样做:

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

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

$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);
    }
};