控制器中的文件说未定义

时间:2016-06-07 09:49:30

标签: angularjs

上传文件时遇到问题。我创建了一项服务:

.directive('fileModel', ['$parse', function ($parse) {
return {
    restrict: 'A',
    link: function(scope, element, attrs) {
        var model = $parse(attrs.fileModel);
        var modelSetter = model.assign;

        element.bind('change', function(){
            scope.$apply(function(){
                modelSetter(scope, element[0].files[0]);
            });
        });
    }
};}]);

然后我有一个指令:

$scope.uploadFile = function(){
    var file = $scope.myFile;
    console.log('file is ' );
    console.dir(file);
    var uploadUrl = "/fileUpload";
    fileUpload.uploadFileToUrl(file, uploadUrl);
};

在控制器中:

Browse <input type="file" file-model="myFile"/>
<button ng-click="uploadFile()">upload me</button>

HTML:

Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.ejb.entidades.Provincia.ciudades[com.ejb.entidades.Ciudad]

正如您所看到的,我在控制器中使用console.log,而我正在使用的是

  

文件未定义

提前致谢!

1 个答案:

答案 0 :(得分:1)

更改

    .directive('fileModel', ['$parse', function ($parse) {
return {
    restrict: 'A',
scope: {
        percent: "=fileModel"

    },
    link: function(scope, element, attrs) {
        var model = $parse(attrs.fileModel);
        var modelSetter = model.assign;
scope.$watch('percent', function(value) {
console.log(value)
});

        element.bind('change', function(){
            scope.$apply(function(){
                modelSetter(scope, element[0].files[0]);
            });
        });
    }
};}]);

在上传文件

后检查百分比监视内的控制台值