如何正确地将数据从服务传递给控制器

时间:2021-05-11 15:10:08

I am trying to send data from my http service to my controller. The service correctly gets the data but it doesn't get sent to the controller.

我正在尝试从我的http服务向我的控制器发送数据。服务正确地获取数据,但是它不会被发送到控制器。

Now, I am aware that the query is done asynchronously which is why I am trying to use $q.defer. I tried following the example provided by a similar question : AngularJS $http call in a Service, return resolved data, not promises , however it still doesn't work.

现在,我知道查询是异步完成的,这就是为什么我要使用$q. deferred。我尝试遵循一个类似的问题提供的示例:在服务中调用AngularJS $http调用,返回已解析的数据,而不是承诺,但是仍然不起作用。

Here is my Service :

以下是我的服务:

.service("builds", ['$http', '$q', function($http, $q) {
  var deferred = $q.defer();

  $http({
      method:'GET',
      url: '/builds',
      cache : true
  }).success(function(data) {
      deferred.resolve(data);
  }).error(function(msg){
      deferred.reject(msg);
  });
  console.log(deferred.promise);
  return deferred.promise;}]);

And here is my routeProvider

这是我的路线提供者

$routeProvider.
    when('/builds', {
        controller: ['$scope', 'buildsData', function ($scope, buildsData) {
            console.log("In routeprovider:" + buildsData);
            $scope.allBuilds = buildsData;
        }],
      template: '<build-list></build-list>',
      resolve: {
          buildsData: ['builds', function(builds){
              return builds;
          }]
      }
    })

And finally here is a snippet of my Controller :

最后是我的控制器的一个片段:

var app = angular.
 module('buildList').
  component('buildList', {
   templateUrl: 'build-list/build-list.template.html',
    controller: function BuildListController($scope, $window,$location,$cookies, builds) {
     console.log($scope.allBuilds);
     $scope.league = $scope.allBuilds;

2 个解决方案

#1


2  

As @vishal says

@vishal说

You should create a method in service because generally a service may have many get and set methods ( I mean best practice).

您应该在服务中创建一个方法,因为通常服务可能有许多get和set方法(我的意思是最佳实践)。

create a function say getData

创建一个函数,比如getData

function getData()
{
  $http({
      method:'GET',
      url: '/builds',
      cache : true
  })
}

then you should be calling this method in controller

那么你应该在控制器中调用这个方法

In the controller you should inject this service and then

在控制器中,您应该先注入此服务,然后再注入

builds.getData().then(function(s){
//result

},function(e){

//error
}
);

#2


0  

you shouldntt have

你shouldntt

 controller: ['$scope', 'buildsData', function ($scope, buildsData) {
            console.log("In routeprovider:" + buildsData);
            $scope.allBuilds = buildsData;
        }],

and a controller in an other file:

另一个文件中的控制器:

You can directly do

你可以直接做

 when('/builds', {
        controller: 'BuildListController'
      template: '<build-list></build-list>',
      resolve: {
          buildsData: ['builds', function(builds){
              return builds;
          }]
      }
    })

and then in your controller

然后在控制器中

$scope.allBuilds = buildsData;

Beside, if you want add some functions to it , your service should look,like this:

此外,如果您想添加一些功能,您的服务应该如下:

.service("builds", ['$http', '$q', function($http, $q) {

      var deferred = $q.defer();

    getbuilds: function(){
      $http({
          method:'GET',
          url: '/builds',
          cache : true
      }).success(function(data) {
          deferred.resolve(data);
      }).error(function(msg){
          deferred.reject(msg);
      });
      console.log(deferred.promise);
      return deferred.promise;}]);
    }

#1


2  

As @vishal says

@vishal说

You should create a method in service because generally a service may have many get and set methods ( I mean best practice).

您应该在服务中创建一个方法,因为通常服务可能有许多get和set方法(我的意思是最佳实践)。

create a function say getData

创建一个函数,比如getData

function getData()
{
  $http({
      method:'GET',
      url: '/builds',
      cache : true
  })
}

then you should be calling this method in controller

那么你应该在控制器中调用这个方法

In the controller you should inject this service and then

在控制器中,您应该先注入此服务,然后再注入

builds.getData().then(function(s){
//result

},function(e){

//error
}
);

#2


0  

you shouldntt have

你shouldntt

 controller: ['$scope', 'buildsData', function ($scope, buildsData) {
            console.log("In routeprovider:" + buildsData);
            $scope.allBuilds = buildsData;
        }],

and a controller in an other file:

另一个文件中的控制器:

You can directly do

你可以直接做

 when('/builds', {
        controller: 'BuildListController'
      template: '<build-list></build-list>',
      resolve: {
          buildsData: ['builds', function(builds){
              return builds;
          }]
      }
    })

and then in your controller

然后在控制器中

$scope.allBuilds = buildsData;

Beside, if you want add some functions to it , your service should look,like this:

此外,如果您想添加一些功能,您的服务应该如下:

.service("builds", ['$http', '$q', function($http, $q) {

      var deferred = $q.defer();

    getbuilds: function(){
      $http({
          method:'GET',
          url: '/builds',
          cache : true
      }).success(function(data) {
          deferred.resolve(data);
      }).error(function(msg){
          deferred.reject(msg);
      });
      console.log(deferred.promise);
      return deferred.promise;}]);
    }