【问题标题】:How to extract multiple requests function to services/factory. angularjs如何将多个请求功能提取到服务/工厂。 angularjs
【发布时间】:2018-02-14 01:54:35
【问题描述】:

我需要向 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

【问题讨论】:

  • 你在控制器内部怎么调用?
  • 我已经更新了包含控制器文件的代码。请检查一下
  • 检查我的回答你错过了退货声明。

标签: javascript angularjs promise xmlhttprequest


【解决方案1】:

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

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);
});

【讨论】:

  • 男人...爱你!原来问题是没有return。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2015-07-22
  • 1970-01-01
  • 2013-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-25
  • 2017-04-18
相关资源
最近更新 更多