如何向服务/工厂提取多个请求功能。 angularjs

时间:2018-02-13 17:38:30

标签: javascript angularjs promise xmlhttprequest

我需要向worldbank api提出多个请求。我搜索过它,我得到了一些帮助我的answer。但我需要在工厂中提取它,我不知道在控制器上返回什么。

阵列

        var indicatorsArray = [

        {indicatorId: "AG.LND.TOTL.K2", name: "Land Area, Total (km²)"},
        {indicatorId: "AG.LND.TOTL.UR.K2", name: "Land Area, Urban (km²)"},
        {indicatorId: "AG.LND.TOTL.RU.K2", name: "Land Area, Rural (km²)"},
        {indicatorId: "AG.LND.FRST.K2", name: "Land Area, Forest (km²)"},
        {indicatorId: "SP.POP.TOTL", name: "Population, Total"},
        {indicatorId: "SP.POP.GROW", name: "Population Growth"},
        {indicatorId: "SP.POP.TOTL.FE.ZS", name: "Population, Female (%)"},
        {indicatorId: "SP.POP.TOTL.MA.ZS", name: "Population, Male (%)"},
        {indicatorId: "SP.DYN.LE00.MA.IN", name: "Life Expectancy, Male (years)"},
        {indicatorId: "SP.DYN.LE00.FE.IN", name: "Life Expectancy, Female (years)"},
        {indicatorId: "SP.DYN.AMRT.MA", name: "Mortality, Male (1/1000)"},
        {indicatorId: "SP.DYN.AMRT.FE", name: "Mortality, Female (1/1000)"}


    ];

factory.js

    function getIndicatorsData(country) {

        var getIndicators = function(indicatorId, name) {
            return $http.get("https://api.worldbank.org/v2/countries/" + country + "/indicators/" + indicatorId + "?MRV=5&Gapfill=Y&format=json")
            .then(getIndicatorsComplete);

            function getIndicatorsComplete(response) {
                return {
                    "indicatorValue": response.data
                }
            };
        };

        var promises = [];

        angular.forEach(indicatorsArray, function(indicator) {
            promises.push(getIndicators(indicator.indicatorId));
        });

        $q.all(promises).then(function(indicators) {
            vm.bankData = vm.bankData.concat(indicators);
        });
    };
}

controller.js

(function(){
    'use strict';

    angular
        .module('country-detail')
        .controller('CountryDetailController', CountryDetailController)

    CountryDetailController.$inject = ['$location', '$window', '$http','$stateParams', 'countryDataService', 'worldBankService'];

    function CountryDetailController($location, $window, $http, $stateParams,countryDataService worldBankService) {
        /* jshint validthis:true */

        var vm = this;
        vm.country = [];
        vm.bankData = [];

        activate();

        function activate() {
            return getCountry().then(function(response) {
                vm.bankData = getIndicatorsData();

            });
        };

        function getCountry() {
            return countryDataService.getCountry(vm.countryCode)
                .then(function(data) {
                    vm.country = data;
                    return vm.country;
                });
       };



        function getIndicatorsData() {
            return worldBankService.getIndicatorsData(vm.countryCode)
                .then(function(data) {
                    vm.bankData = data;
                    return vm.bankData;
                });
        };
    }
})()

我试图在控制器上返回数据,但是出现了一些错误。

我想知道我的工厂是否存在问题,以及如何在控制器上返回数据。

最好的解决方案让我知道如何在工厂中提取this

1 个答案:

答案 0 :(得分:0)

你没有从工厂返回任何东西

function getIndicatorsData(country) {
    var getIndicators = function (indicatorId, name) {
        return $http.get("https://api.worldbank.org/v2/countries/" + country + "/indicators/" + indicatorId + "?MRV=5&Gapfill=Y&format=json")
            .then(getIndicatorsComplete);

        function getIndicatorsComplete(response) {
            return {
                "indicatorValue": response.data
            }
        };
    };

    var promises = [];

    angular.forEach(indicatorsArray, function (indicator) {
        promises.push(getIndicators(indicator.indicatorId));
    });

    return $q.all(promises);
}

内部控制器

getIndicatorsData().then(function (indicators) {
    console.log(indicators);
});