0

Here is my controller and service:

var app = angular.module('myApp', ['ui.bootstrap']);

app.service("BrandService", ['$http', function($http){
this.reloadlist = function(){
    var list;
     $http.get('/admin.brands/getJSONDataOfSearch').
        success(function(data, status, headers, config) {
          list = data;
        }).
        error(function(data, status, headers, config) {

        });
     return list;
};

}]);

app.controller('BrandsCtrl', ['$scope','$http','$controller','BrandService', function($scope, $http, $controller, BrandService) {
    $scope.brands = BrandService.reloadlist();
    angular.extend(this, $controller("BrandCtrl", {$scope: $scope}));
}]);

I searched for this issue and tried answers of questions but I couldn't get solution. I am new at angular so can you explain with details; why I couldn't get the data from service to controller this way ?

3 Answers 3

1

The return used for data is for the callback of your function. You must use the promise returned by $http like this.

In your service return the promise :

return  $http.get('/admin.brands/getJSONDataOfSearch').
            success(function(data, status, headers, config) {
              return data;
            }).
            error(function(data, status, headers, config) {

            });

Use then() on the promise in your controller :

  BrandService.reloadlist()
     .then(function (data){
       $scope.brands = data;      
     });
Sign up to request clarification or add additional context in comments.

Comments

1

It's not angular, it's the Javascript. The function you put in this.reloadlist does not return any value. It has no return at all, so the value returned will be undefined. The success handler does return something, but it will be run long after reloadlist finished working.

1 Comment

your update is completely wrong (did you try to understand the advice?...). You still returned list before the data was read. (quote: "it will be run long after reloadList finished working").
1

Besides what @fdreger already pointed out (missing return value), $http.get(...) is an async method. The return value is a promise not the actual value. In order to access the value you need to return it from reloadlist like this:

this.reloadList = function() {
  return $http.get('/admin.brands/getJSONDataOfSearch');
  // you need to handle the promise in here. You could add a error handling here later by catching stuff...
}

and in the controller you can add it to the $scope like this:

BrandService
  .reloadlist()
  .then(function(res) {
    $scope.brands = res.data;
  });

The callback passed to then() is called as soon as the HTTP request has successfully completed, this makes the call asynchronous.

Besides the angular documentation for promises the article on MDN is a good read too.

2 Comments

What is promise ? Why do I need to use it ?
@GaripTipici: this qualifies as another question . Starting angularjs without knowing JavaScript and at least trying to read up on promises is probably too many things at once.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.