AngularJs调用从自我内部服务功能 [英] AngularJs call an internal service function from self

查看:116
本文介绍了AngularJs调用从自我内部服务功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有构建一个简单的服务,使多个请求。该服务有两个方法。我不能从另一个调用一种方法里面的服务。

I have build a simple service that makes multiple requests. The service has two methods. I cannot call one method from another inside the service.

Plunkr:<一href=\"http://plnkr.co/edit/2fERik4uTxbxlVOhncMd?p=$p$pview\">http://plnkr.co/edit/2fERik4uTxbxlVOhncMd?p=$p$pview

app.factory('Report', ['$http', function($http){
var Authors = {

    reports : [],
    requests :[{'url':'data/data.cfm','response':'first'},
               {'url':'data.json','response':'second'},
               {'url':'data.json','response':'third'},
               {'url':'data.json','response':'forth'}],


getReport : function(target, source, response, callback) {
    return $http({  url:source, 
                    method:"POST", 
                    params:{url : target}
                }).success(function(result) {
                    $scope.progress = response;
                    angular.extend($scope.user, result)
                    console.log($scope.user)
      }
      ).error(function(error){
                    $scope.progress = response
                })
},

    startQueue : function (target) {
        var promises = [];
        this.requests.forEach(function (obj, i) {
            console.log(obj.url)
            promises.push(getReport(target, obj.url, obj.response, function(value){
                reports.push(value);
                console.log(value)
            }));
        });
        $q.all(promises).then(function () {
            console.log("Finito");
        },function(error){
            console.log("errori")
        });
    }

};

return Authors;
}])

当我尝试调用getReport从内startQueue我得到错误:getReport没有定义

When I try to call getReport from inside startQueue I get error: getReport is not defined.

推荐答案

您的工厂更改为:

app.factory('Report', ['$http', function($http){
    var Authors = {

        reports : [],
        requests :[{'url':'data/data.cfm','response':'first'},
                   {'url':'data.json','response':'second'},
                   {'url':'data.json','response':'third'},
                   {'url':'data.json','response':'forth'}],
    };

    Authors.getReport = function(target, source, response, callback) {

    };
    Authors.startQueue = function (target) {

    };

    return Authors;
}])

这篇关于AngularJs调用从自我内部服务功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆