Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it ok to do the $http get request on ui.router's resolve in angularjs?

I have the following code (below), they work perfectly for me and to what I need at least. But Im kind of skeptical about this, Im having a feeling that its too good to be true. Since Im struggling with $http's async behavior this helped me a lot to use the response object from the $http request globally on the controller.

I just want to know if its the right way or at least an acceptable one or should I use the conventional way of using $http get like the one on AngularJS' Documentation before I move on with my project. Answers will help me a lot. Thank you.

$stateProvider

$stateProvider
    .state('test', {
        url: '/test',
        templateUrl: 'partial.template.html',
        resolve : {
            foo : function($http) {
                return $http({
                    method: 'GET',
                    url: '/api/something'
                });
            },
            bar : function($http) {
                return $http({
                    method: 'GET',
                    url: '/api/something'
                });
            },
        },
        controller: 'mainController',
    })

controller

.controller('mainController',['$scope', 'foo', 'bar', function($scope, foo, bar){
    $scope.fooObj = foo;
    $scope.barObj = bar;
}])
like image 847
CENT1PEDE Avatar asked Jul 22 '15 12:07

CENT1PEDE


People also ask

What is $HTTP service in AngularJS?

The $http service is a core AngularJS service that facilitates communication with the remote HTTP servers via the browser's XMLHttpRequest object or via JSONP. For unit testing applications that use $http service, see $httpBackend mock. For a higher level of abstraction, please check out the $resource service.

What is UI router in AngularJS?

Angular-UI-Router is an AngularJS module used to create routes for AngularJS applications. Routes are an important part of Single-Page-Applications (SPAs) as well as regular applications and Angular-UI-Router provides easy creation and usage of routes in AngularJS.


1 Answers

I think this is probably the best usecase for a ui-router resolve.

Anyway i'd prefer to wrap my http call into services and call this services into the resolve instead of using $http directly.

This may look like this :

app.service('FooService',function($http){
  var service={}; 
  service.getFoo = function(){
      return $http({
                method: 'GET',
                url: '/api/something'
             });
  }
  return service;
});

Thanks to that you can call this method all around your application (and centralize it at the same time).

In controller :

app.controller('MyController', function($scope, FooService) {
    $scope.controllerName = "MyController";
    FooService.getFoo().success(function(foo){
        $scope.foo = foo
    });
});

In a resolve :

$stateProvider
.state('test', {
    url: '/test',
    templateUrl: 'partial.template.html',
    resolve : {
        foo : function(FooService) {
            return FooService.getFoo();
        },
    },
    controller: 'MyController',
})

Go on with this approach, you're on a good way.

Hope it helped.

EDIT : Built a plunker to add a concrete exemple of theses methods.

like image 79
Okazari Avatar answered Oct 03 '22 08:10

Okazari