小编ice*_*erg的帖子

AngularJS:将数据从服务返回到控制器

我正在尝试创建一个服务来获取json并将其传递给我homeCtrl我可以获取数据但是当它传递给我的homeCtrl它总是返回undefined.我卡住了.

我的服务:

var myService = angular.module("xo").factory("myService", ['$http', function($http){
  return{
    getResponders: (function(response){
      $http.get('myUrl').then(function(response){
         console.log("coming from servicejs", response.data);
      });
    })()
  };
  return myService;
  }
]);
Run Code Online (Sandbox Code Playgroud)

我的家庭控制器:

var homeCtrl = angular.module("xo").controller("homeCtrl", ["$rootScope", "$scope", "$http", "myService",
function ($rootScope, $scope, $http, myService) {
 $scope.goData = function(){
     $scope.gotData = myService.getResponders;
 };
 console.log("my service is running", $scope.goData, myService);
}]);
Run Code Online (Sandbox Code Playgroud)

angularjs angularjs-service angularjs-factory angularjs-http angular-promise

8
推荐指数
1
解决办法
4万
查看次数