Search code examples
javascriptangularjsangular-ui-routerangularjs-serviceangularjs-factory

Cannot get data from service after migrating to AngularJS 1.6.3


After migrating to AngularJS 1.6.3, I changed my services like below:

Here is my service:

MetronicApp.factory('MyService', ['$http', function($http) {
    return {
    get: function(id, success, error) {
        return $http.get(baseUrl + '/quotation/' + quotationid).then(success,error);
    }
  }
}]);

and in ui-router, I'm resolving the data like this.

data: function(MyService, $stateParams) {
    return MyService.get($stateParams.id);
}

But in the controller, data comes undefined. Where am I wrong?


Solution

  • It is because you're not returning anything from success/error callback functions. Rather I'd suggest to remove .then(success,error); from get method. and then just call service get method from resolve.

    data: function(MyService, $stateParams) {
        return MyService.get($stateParams.id);
    }