AngularJS作为工厂监听器的范围

时间:2014-10-12 09:50:45

标签: angularjs

我有一个只返回上传助手对象的工厂。此帮助程序将文件上载到服务器,并将upload-progress返回给其侦听器。 注入工厂的控制器是监听器,通过设置回调方法到upload-helper。 控制器获取值但不使用新值更新其范围。

这是工厂:

app.factory("upHelper", function(){

    var uploadHelper = function(){
    }
    uploadHelper.prototype.addFileToUpload = function(file){
           this.file = file;
    }
    uploadHelper.prototype.stopUpload = function(){....}
    uploadHelper.prototype.setListener = function(cb){
        var self = this;  
        cb(self.getStatus());
    } 
    var upHelper= new uploadHelper();

    return self.uploadHelper;
}

带工厂注射的控制器:

app.controller("upload", function("upHelper"){
    $scope.file = file;

    $scope.uploadFile = function(file){
        uploadHelper.addFileToUpload($scope.files);
        if(!uploadHelper.running)
              uploadHelper.start():
        uploadHelper.setListener(function(uploadStatus){
               $scope.file.uploadStatus = uploadStatus;    // this doesn't update the scope
               $scope.apply(function(){
                    $scope.file.uploadStatus = uploadStatus;   //causes error and doesn't update anything.
               });


        }
    }
}

有几个控制器使用uploadHelper ..有没有更好的方法来实现这个功能,还是让它工作?

0 个答案:

没有答案