是否可以在uj.router的angularjs中解析$ http get请求? [英] Is it ok to do the $http get request on ui.router's resolve in angularjs?

查看:195
本文介绍了是否可以在uj.router的angularjs中解析$ http get请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码(下面),它们对我和我至少需要的东西都很有效。但我对此持怀疑态度,我感觉它太好了,不可能。由于我正在努力解决 $ http 的异步行为,这对我使用 $ http 中的响应对象有很大帮助在控制器上全局请求。

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.

我只是想知道它是正确的方式还是至少是可接受的方式,或者我应该使用传统方式使用 $ http 在我移动之前得到像 AngularJS'文档上的那个我的项目。答案对我很有帮助。谢谢。

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('mainController',['$scope', 'foo', 'bar', function($scope, foo, bar){
    $scope.fooObj = foo;
    $scope.barObj = bar;
}])


推荐答案

我认为这可能是ui的最佳用例路由器解析。

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

无论如何,我更愿意将我的http呼叫包装到服务中并将此服务调用到解析中,而不是直接使用$ http。

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

这可能如下所示:

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).

在控制器中:

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

决心:

$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.

希望有所帮助。

编辑:建立 plunker 以添加这些方法的具体例子。

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

这篇关于是否可以在uj.router的angularjs中解析$ http get请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆