Angular js文件上传

时间:2018-11-01 08:45:55

标签: angularjs file-upload

我正在尝试在php中使用$ _FILES访问文件参数,但无法在angular js控制器中获取文件的值

下面的查看文件是我输入的文件

<input type="file" filestyle style="" ng-model="final_data['custom_question_order_level'][k]['answer']" name="order_custom_field_answer{{k}}" valid-file>

这是我对angular js的指令代码,位于指令.js文件中validFile是我在我的directive.js文件中传递的指令名称

    myApp.directive('validFile', ['$parse', function ($parse) {
        return {
            require: 'ngModel',
            scope: {
                validFile: '='
            },
            // link function
            link: function (scope, el, attrs, ctrl) {
                var ext = el.val().split('.').pop();
                ctrl.$setValidity('validFile', el.val() != '' && (ext == "png" || ext == "pdf" || ext == "jpg" || ext == "jpeg"));
                el.bind('change', function (event) {
                    var ext = el.val().split('.').pop();
                    ctrl.$setValidity('validFile', el.val() != '' && (ext == "png" || ext == "pdf" || ext == "jpg" || ext == "jpeg"));
                    // get the file values upload from view
                    var file = event.target.files[0];
                    console.log(file);
                    scope.file = file ? file : "undefined";
                    // passing the scope
                    scope.$apply(function () {
                        //pass the values in scope
                        ctrl.$setViewValue(el.val());
                        ctrl.$render();
                    });
                });
            }
        }
    }]);

0 个答案:

没有答案