AngularJS从服务URL获取数据

AngularJS从服务URL获取数据,angularjs,Angularjs,我是新来安格拉斯的。 我从URL下载AngularJS主题 我必须从get服务获取数据。 获取服务URL如下所示 (function () { angular .module('app') .controller('VisitorsController', [ VisitorsController ]); function VisitorsController() { var vm = th

我是新来安格拉斯的。 我从URL下载AngularJS主题

我必须从get服务获取数据。 获取服务URL如下所示

(function () {
    angular
        .module('app')
        .controller('VisitorsController', [
            VisitorsController
        ]);

    function VisitorsController() {
        var vm = this;
        // TODO: move data to the service
        var servicesUrl = app.serviceCallUrl+'getHostStatus';
        console.log(servicesUrl);
        var myService = angular.module("app").factory("myService", ['$http', function($http) {
            return {
                getResponders: (function(response) {
                    return $http.get(servicesUrl).then(function(response) {
                        console.log(response.data);
                        return response.data;
                    });
                })()
            };
            return myService;
        }]);

        vm.visitorsChartData = [ {key: 'UP', y: 5264}, { key: 'Critical', y: 3872},{key: 'Warning', y: 1000} ];

        vm.chartOptions = {
            chart: {
                type: 'pieChart',
                height: 210,
                donut: true,
                x: function (d) { return d.key; },
                y: function (d) { return d.y; },
                valueFormat: (d3.format(".0f")),
                color: ['rgb(0, 150, 136)', '#E75753','#fbbc05'],
                showLabels: false,
                showLegend: false,
                title: 'Over 9K',
                margin: { top: -10 }
            }
        };
    }
})();

运行正常

因此,我必须使用此URL并从该服务获取数据,我只需更改我的VisitorsController.js中的代码即可 VisitorsController.js文件代码如下所示

(function () {
    angular
        .module('app')
        .controller('VisitorsController', [
            VisitorsController
        ]);

    function VisitorsController() {
        var vm = this;
        // TODO: move data to the service
        var servicesUrl = app.serviceCallUrl+'getHostStatus';
        console.log(servicesUrl);
        var myService = angular.module("app").factory("myService", ['$http', function($http) {
            return {
                getResponders: (function(response) {
                    return $http.get(servicesUrl).then(function(response) {
                        console.log(response.data);
                        return response.data;
                    });
                })()
            };
            return myService;
        }]);

        vm.visitorsChartData = [ {key: 'UP', y: 5264}, { key: 'Critical', y: 3872},{key: 'Warning', y: 1000} ];

        vm.chartOptions = {
            chart: {
                type: 'pieChart',
                height: 210,
                donut: true,
                x: function (d) { return d.key; },
                y: function (d) { return d.y; },
                valueFormat: (d3.format(".0f")),
                color: ['rgb(0, 150, 136)', '#E75753','#fbbc05'],
                showLabels: false,
                showLegend: false,
                title: 'Over 9K',
                margin: { top: -10 }
            }
        };
    }
})();
当我运行此命令时,它会在控制台上显示正确的
servicesUrl
。 但
响应。控制台上不显示数据


因此,请指导我如何解决它。

您是否尝试了
response.json().data
?编写一个
服务
作为单独的函数,并插入
控制器
,然后尝试使用。@RVP我尝试了这个,但没有任何进展。@AvneshShakya那么我应该创建新的文件app.service吗?好的,我可能是盲的,但你真的调用了你定义的方法吗?是否喜欢运行myService.getResponders()