返回由两个资源调用的$承诺在AngularJS推迟路由变化 [英] Returning a $promise from two resource calls in AngularJS to delay route change

查看:156
本文介绍了返回由两个资源调用的$承诺在AngularJS推迟路由变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用路由提供商,在决心返回$承诺将避免数据弹出的界面加载之后。然而,我无法处理两个不同的资源调用和返回它作为一个单一的阵列。

Using a route provider, returning a $promise in the resolve will avoid the data popping up after the UI has loaded. However, I'm having trouble processing two different resource calls and returning it as a single array.

的当前实现工作,但要求该控制器需要两个资源。

The current implementation works, but requires that the controller requires two resources.

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

app.config(function($routeProvider) {
  $routeProvider
    .when('/resource/:id', {
      templateUrl: 'partials/resource.html',
      controller: ResourceController,
      resolve: {
        parentResource: function($route, SingleResource) {
          return SingleResource.get({ id: $route.current.params.id }).$promise;
        },
        childResources: function($route, ChildResource) {
          return ChildResource.get({ id: $route.current.params.id }).$promise;
        }
      }
  });
 });

不过,我希望做的是提供 ResourceController 与单个阵列资源这是一个数组 parentResource 的组成和 childResource ,其中数组中的项目0是 parentResource

However, what I'd like to do is provide ResourceController with a single array Resource that is an array composed of parentResource and childResource where item 0 in the array is the parentResource.

这是我的想法,但我不知道在决心回来吗?如果我返回结果,路线会改变,传递一个空数组。

This is what I have in mind, but I don't know what to return in the resolve? If I return result, the route will change and pass an empty array.

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

app.config(function($routeProvider) {
  $routeProvider
    .when('/resource/:id', {
      templateUrl: 'partials/resource.html',
      controller: ResourceController,
      resolve: {
        Resource: function($route, SingleResource) {
          var resources = [];
          SingleResource.get({ id: $route.current.params.id })
            .$promise.then(function(parent)
                             { resources.splice(0, 0, parent); })
                     .catch(function(error) { alert('error!'); })
          ChildResource.get({ id: $route.current.params.id })
            .$promise.then(function(children) 
                             { resources = resources .concat(children); })
                     .catch(function(error) { alert('error!'); })

          return ???? // Can I return a $promise here?
        }
      }
  });
});

问题

如何我可以从我得到的签名ResourceController
我们的想法是,然后让该控制器有这个签名:

Question

How can I get my ResourceController signature from The idea is then to get the controller to have this signature:

var ResourceController = function($scope, parentResource, childResources) { ... }

var ResourceController = function($scope, resources) { ... }

通过返回$承诺?

through returning $promise?

推荐答案

您可以用$ q.all返回承诺的数组

You can use $q.all to return an array of promises

         var resources = [];

         resources.push(SingleResource.get({ id: $route.current.params.id }).$promise);
         resources.push(ChildResource.get({ id: $route.current.params.id }).$promise);

         return $q.all(resources) // You return an array of promises

这篇关于返回由两个资源调用的$承诺在AngularJS推迟路由变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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