Javascript 使用另一个中声明为模块模式的服务方法之一

Javascript 使用另一个中声明为模块模式的服务方法之一,javascript,angularjs,Javascript,Angularjs,我有一个像这样的角度服务: videoApp.factory('cacheLoader', function ($http, $filter, $rootScope) { this.self = this; return { load: function (url, allowCache) { if(allowCache == false || localStorage.getItem(url) && (parseInt(l

我有一个像这样的角度服务:

videoApp.factory('cacheLoader', function ($http, $filter, $rootScope) {
    this.self = this;
    return {
        load: function (url, allowCache) {
            if(allowCache == false || localStorage.getItem(url) && (parseInt(localStorage.getItem(url + 'time')) + 20000) < (new Date().getTime()) || (!localStorage.getItem(url) )) {
                $http.get(url).success(function (data) {

                    $rootScope.allData = data;
                    $rootScope.videos = data;

                    $rootScope.categories = $filter('categoryFilter')(data);

                    if(allowCache == true && parseInt(localStorage.getItem(url + 'time')) + 20000 < (new Date().getTime() )) {
                        localStorage.setItem(url, JSON.stringify(data));
                        localStorage.setItem(url + 'time', new Date().getTime());
                    }

                });
            } else {
                $rootScope.allData = JSON.parse(localStorage.getItem(url));
                $rootScope.videos = JSON.parse(localStorage.getItem(url));
                $rootScope.categories = $filter('categoryFilter')(JSON.parse(localStorage.getItem(url)));
            }
        },
        getFilteredResults: function (category, data, callback) {
            callback = callback || $filter('articleFilter');
            $rootScope.videos = callback(category, data);
            return $rootScope.videos;
        }
    };
});
videoApp.factory('cacheLoader',函数($http,$filter,$rootScope){
this.self=这个;
返回{
加载:函数(url、allowCache){
如果(allowCache==false | | | | localStorage.getItem(url)和&(parseInt(localStorage.getItem(url+“time”))+20000)<(new Date().getTime())| |(!localStorage.getItem(url))){
$http.get(url).success(函数(数据){
$rootScope.allData=数据;
$rootScope.videos=数据;
$rootScope.categories=$filter('categoryFilter')(数据);
if(allowCache==true&&parseInt(localStorage.getItem(url+'time'))+20000<(new Date().getTime()){
setItem(url,JSON.stringify(数据));
setItem(url+'time',new Date().getTime());
}
});
}否则{
$rootScope.allData=JSON.parse(localStorage.getItem(url));
$rootScope.videos=JSON.parse(localStorage.getItem(url));
$rootScope.categories=$filter('categoryFilter')(JSON.parse(localStorage.getItem(url));
}
},
getFilteredResults:函数(类别、数据、回调){
callback=callback | |$filter('articleFilter');
$rootScope.videos=回调(类别、数据);
返回$rootScope.videos;
}
};
});
现在,我想在cacheLoader.load函数中使用cacheLoader.getFilteredResults。这是一个窗口对象,所以我不能指向它,我试图将它绑定到整个模块模式,但它抛出了错误


有没有办法完成我想做的事情

您应该能够在
load
方法中使用
this.getFilteredResults

videoApp.factory('cacheLoader', function ($http, $filter, $rootScope) {
    this.self = this;
    return {
        load: function () {
            this.getFilteredResults();
        },
        getFilteredResults: function () {
            alert('hi');
        }
    };
});
如果您需要在回调中处理引用此,请执行以下操作:

return {
    load: function () {
        var that = this;
        doSomeThing().then(function(data) {
            that.getFilteredResults();
        });
    },
    getFilteredResults: function () {
        alert('hi');
    }
};