AngularJS:在modal ui引导程序中上载文件

AngularJS:在modal ui引导程序中上载文件,angularjs,scope,modal-dialog,angular-ui-bootstrap,Angularjs,Scope,Modal Dialog,Angular Ui Bootstrap,我有一个角度模态用户界面,在其中我上传文件。问题是由于某种原因,文件的不会触发app指令。当更改时,该指令返回文件名和大小 这是我想要得到的结果: 我真的尝试过任何东西,但由于某种原因,我仍然无法在中看到文件名 html文件: <form novalidate ng-submit="add(Form.$valid)" name="Form"> <div class="modal-header col-lg-12"> <h3 class="c

我有一个角度模态用户界面,在其中我上传文件。问题是由于某种原因,文件的
不会触发app指令。当更改
时,该指令返回文件名和大小

这是我想要得到的结果:

我真的尝试过任何东西,但由于某种原因,我仍然无法在
中看到文件名

html文件:

<form novalidate ng-submit="add(Form.$valid)" name="Form">
    <div class="modal-header col-lg-12">
        <h3 class="col-lg-4 col-lg-offset-4">add file</h3>
    </div>
    <div class="modal-body">
        <div class="panel-body">
            <div class="row">
                <div class="form-group col-lg-8" ng-class="{'has-error': notPass && Form.fileName.$invalid}">
                    <label class="control-label label-add-card" for="fileName">name</label>
                    <input class="input-add-card form-control " id="fileName" name="fileName" type="text"  ng-model="fileName" ng-pattern="/^[a-z1-9]{10,}$/" ng-required="true">
                    <p ng-show="notPass && Form.fileName.$invalid" class="help-block">not good</p>
                </div>
            </div>
            <div class="row">
                <div class="form-group col-lg-8" ng-class="{'has-error':notPass && Form.fileExplain.$invalid}">
                    <label class="control-label label-add-card" for="fileExplain">explain</label>
                    <textarea class="input-big form-control " id="fileExplain" name="fileExplain" type="text"  ng-model="fileExplain" ng-pattern="/^[a-z1-9]{1,}$/" ng-required="true"></textarea>
                    <p ng-show="notPass && Form.fileExplain.$invalid" class="help-block">not good</p>
                </div>
            </div>
            <div>
                <div class="form-group col-lg-12" ng-class="{'has-error':notPass && Form.uploadDownloads.$invalid}">

                    <input  ng-model="uploadDownloads" type="file" fd-input file-name="fileName" />
                    <input  class="btn btn-primary" type="button" value="choose" onclick="$(this).parent().find('input[type=file]').click();"/> <!-- on button click fire the file click event -->
                    <span class="badge badge-important">{{fileName}}</span>
                    <p ng-show="notPass && Form.uploadDownloads.$invalid" class="help-block">please choose</p>
                </div>
            </div>
        </div>
    </div>
    <div class="modal-footer">
        <button type="submit" class="btn btn-success col-lg-12 btn-modal-costume">add</button>
    </div>
</form> 

我是这样做的:

html:

<form novalidate ng-submit="add(Form.$valid)" name="Form">
.
.
.
<div>
                <div class="form-group col-lg-12" ng-class="{'has-error':notPass && Form.uploadDownloads.$invalid}">

                    <input  ng-model="uploadDownloads" class="form-control-file" id="uploadDownloads" type="file" fd-input file-names="fileNames" />
                    <input  class="btn btn-primary" type="button" value="choose" ng-click="choose()"/> <!-- on button click fire the file click event -->
                    <span class="badge badge-important">{{fileNames}}</span>
                    <p ng-show="notPass && Form.uploadDownloads.$invalid" class="help-block">you have to choose file</p>
                </div>
            </div>
.
.
.
</form>
这是上传文件控制器:

/**
 * Created by Ariel on 22/11/2015.
 */
app.controller('uploadDownloadsController',function($scope,$modalInstance ){
    app.directive('fdInput', fdInput);
    function fdInput() {
        return {
            scope: {
                fileName: '='
            },
            link: function(scope, element, attrs) {
                element.on('change', function(evt) {
                    var files = evt.target.files;
                    console.log(files[0].name);
                    console.log(files[0].size);

                    scope.fileName = files[0].name;
                    scope.$apply();
                });
            }
        }
    };
    $scope.fileName = '';

    $scope.add = function(valid){
        if(valid){
                $scope.data = 'none';
                var f = document.getElementById('uploadDownloads').files[0];
                var r = new FileReader();
                r.onloadend = function(e){
                    $scope.data = e.target.result;
                    $scope.notPass = false;
                    $modalInstance.close({
                        'data':$scope.data,
                        'fileName':$scope.fileName,
                        'fileExplain':$scope.fileExplain
                    });
                };
            /*activate the onloadend to catch the file*/
                r.readAsBinaryString(f);
        } else {
            $scope.notPass = true;
        }
    };
    $scope.cancel = function() {
        $modalInstance.dismiss('cancel');
    };
});
app.controller('uploadDownloadsController',function($scope,$modalInstance,$timeout){
    $scope.fileNames = '';
    $scope.choose = function(){
        $('#uploadDownloads').trigger('click');
    };
    $scope.add = function(valid){
        if(valid){
            $scope.data = 'none';
            $scope.notPass = false;
/*this catches the file*/
            var fileInput = document.getElementById('uploadDownloads');
            var file = fileInput.files[0];
/* to send the file and the other inputs about it, need to use formData type*/
            var formData = new FormData();
            formData.append('file', file);
            formData.append('fileName', $scope.fileName);
            formData.append('fileExplain', $scope.fileExplain);
            console.log(formData);
            $modalInstance.close(formData);
        } else {
            $scope.notPass = true;
        }
    };
    $scope.cancel = function() {
        $modalInstance.dismiss('cancel');
    };
}); 

我是这样做的:

html:

<form novalidate ng-submit="add(Form.$valid)" name="Form">
.
.
.
<div>
                <div class="form-group col-lg-12" ng-class="{'has-error':notPass && Form.uploadDownloads.$invalid}">

                    <input  ng-model="uploadDownloads" class="form-control-file" id="uploadDownloads" type="file" fd-input file-names="fileNames" />
                    <input  class="btn btn-primary" type="button" value="choose" ng-click="choose()"/> <!-- on button click fire the file click event -->
                    <span class="badge badge-important">{{fileNames}}</span>
                    <p ng-show="notPass && Form.uploadDownloads.$invalid" class="help-block">you have to choose file</p>
                </div>
            </div>
.
.
.
</form>
这是上传文件控制器:

/**
 * Created by Ariel on 22/11/2015.
 */
app.controller('uploadDownloadsController',function($scope,$modalInstance ){
    app.directive('fdInput', fdInput);
    function fdInput() {
        return {
            scope: {
                fileName: '='
            },
            link: function(scope, element, attrs) {
                element.on('change', function(evt) {
                    var files = evt.target.files;
                    console.log(files[0].name);
                    console.log(files[0].size);

                    scope.fileName = files[0].name;
                    scope.$apply();
                });
            }
        }
    };
    $scope.fileName = '';

    $scope.add = function(valid){
        if(valid){
                $scope.data = 'none';
                var f = document.getElementById('uploadDownloads').files[0];
                var r = new FileReader();
                r.onloadend = function(e){
                    $scope.data = e.target.result;
                    $scope.notPass = false;
                    $modalInstance.close({
                        'data':$scope.data,
                        'fileName':$scope.fileName,
                        'fileExplain':$scope.fileExplain
                    });
                };
            /*activate the onloadend to catch the file*/
                r.readAsBinaryString(f);
        } else {
            $scope.notPass = true;
        }
    };
    $scope.cancel = function() {
        $modalInstance.dismiss('cancel');
    };
});
app.controller('uploadDownloadsController',function($scope,$modalInstance,$timeout){
    $scope.fileNames = '';
    $scope.choose = function(){
        $('#uploadDownloads').trigger('click');
    };
    $scope.add = function(valid){
        if(valid){
            $scope.data = 'none';
            $scope.notPass = false;
/*this catches the file*/
            var fileInput = document.getElementById('uploadDownloads');
            var file = fileInput.files[0];
/* to send the file and the other inputs about it, need to use formData type*/
            var formData = new FormData();
            formData.append('file', file);
            formData.append('fileName', $scope.fileName);
            formData.append('fileExplain', $scope.fileExplain);
            console.log(formData);
            $modalInstance.close(formData);
        } else {
            $scope.notPass = true;
        }
    };
    $scope.cancel = function() {
        $modalInstance.dismiss('cancel');
    };
}); 

嗨,我也面临同样的问题。表单数据显示ui引导angularjs插件中的“未定义”。请推荐我,我发布了上传文件的方法,希望它能帮助@user1764882Hi,我也面临同样的问题。表单数据显示ui引导angularjs插件中的“未定义”。请推荐我,我发布了上传文件的方法,希望对@user1764882有所帮助