angularjs - Angular ui-router 解析父状态

标签 angularjs angular-ui-router

在我的父状态中,我有一个解决。目前,我不会在任何子状态下注入(inject) resolve 键。

我假设我的子状态不会等待此解决/ promise 得到解决。但是当我设置超时时,我可以看到我的子状态确实在等待。

这是正确的行为吗?

.config(function ($stateProvider, $urlRouterProvider, STATES) {
    $stateProvider
        .state(STATES.ROOT, {
            abstract: true,
            template:'<div ui-view=""></div>',
            resolve: {                  
                UserService: 'UserService',
               userDetails: function(UserService){
                   return UserService.getUserProfile();
                }

            }
        })
        .state(STATES.BILLING, {
            url: '/bill/checkClientContext.html',
           templateUrl: 'bill/checkClientContext.html',
            controller: 'BillingController'
        })

UserService.js

'use strict';
angular.module('nabc.data')
    .service('UserService', ['AjaxService', '$timeout','$q', function(AjaxService, $timeout, $q) {
        var getUserProfile = function() {
            var promise = AjaxService.get('userProfile');

            var deferred = $q.defer();
            $timeout(function(response){
                deferred.resolve(response);
            }, 5000);
            return deferred.promise;


        };

        return {
            getUserProfile: getUserProfile
        };
    }]);

正如您在上面所看到的,BillingController 不会注入(inject) userDetails 中。但是当我在 userService 中设置超时时,我发现我的计费状态确实在等待。

最佳答案

可以在这里找到答案(让我引用几行代码和片段):

Inherited Resolved Dependencies

0.2.0版本中的新功能

Child states will inherit resolved dependencies from parent state(s), which they can overwrite. You can then inject resolved dependencies into the controllers and resolve functions of child states.

$stateProvider.state('parent', {
      resolve:{
         resA:  function(){
            return {'value': 'A'};
         }
      },
      controller: function($scope, resA){
          $scope.resA = resA.value;
      }
   })
   .state('parent.child', {
      resolve:{
         resB: function(resA){
            return {'value': resA.value + 'B'};
         }
      },
      controller: function($scope, resA, resB){
          $scope.resA2 = resA.value;
          $scope.resB = resB.value;
      }

因此,正如我们在文档中看到的,父级上的解析可以用于子级。这实际上是原因,而我们必须等待这个问题解决......然后才能继续。

但我想说,这是预料之中的。更奇特的功能是:

if the child state has defined resolve - and parent does not - it would be great to see parent views rendered, and let only child's views to wait.

这是(据我所知)计划在不久的将来作为一项功能...

EXTEND - 等待所有人继续就是答案

要得到答案:

Must all the resolves - declared in current state and all its parent states - be awaited to continue with current? Even if current state controllers do not require any of these values?

请遵守代码: state.js

function resolveState(state, params, paramsAreFiltered, inherited, dst, options) {
  // Make a restricted $stateParams with only the parameters that apply to this state if
  // necessary. In addition to being available to the controller and onEnter/onExit callbacks,
  // we also need $stateParams to be available for any $injector calls we make during the
  // dependency resolution process.
  var $stateParams = (paramsAreFiltered) ? params : filterByKeys(state.params.$$keys(), params);
  var locals = { $stateParams: $stateParams };

  // Resolve 'global' dependencies for the state, i.e. those not specific to a view.
  // We're also including $stateParams in this; that way the parameters are restricted
  // to the set that should be visible to the state, and are independent of when we update
  // the global $state and $stateParams values.
  dst.resolve = $resolve.resolve(state.resolve, locals, dst.resolve, state);
  var promises = [dst.resolve.then(function (globals) {
    dst.globals = globals;
  })];
  if (inherited) promises.push(inherited);

  // Resolve template and dependencies for all views.
  forEach(state.views, function (view, name) {
    var injectables = (view.resolve && view.resolve !== state.resolve ? view.resolve : {});
    injectables.$template = [ function () {
      return $view.load(name, { view: view, locals: locals, params: $stateParams, notify: options.notify }) || '';
    }];

    promises.push($resolve.resolve(injectables, locals, dst.resolve, state).then(function (result) {
      // References to the controller (only instantiated at link time)
      if (isFunction(view.controllerProvider) || isArray(view.controllerProvider)) {
        var injectLocals = angular.extend({}, injectables, locals);
        result.$$controller = $injector.invoke(view.controllerProvider, null, injectLocals);
      } else {
        result.$$controller = view.controller;
      }
      // Provide access to the state itself for internal use
      result.$$state = state;
      result.$$controllerAs = view.controllerAs;
      dst[name] = result;
    }));
  });

  // Wait for all the promises and then return the activation object
  return $q.all(promises).then(function (values) {
    return dst;
  });
}

我决定在这里引用完整的方法,但最重要的部分是:var Promise = [];的声明。该数组稍后会扩展为继续 promises.push($resolve.resolve(... ) 所需的所有解析,最后,直到所有内容都没有完成,没有结果- 返回 $q.all(promises)

关于angularjs - Angular ui-router 解析父状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28534015/

相关文章:

javascript - 连接到 angular-ui-router $transitions API 时出现意外的 window.confirm 行为

angularjs - $stateChangeSuccess 被击中两次

javascript - 嵌套状态不会加载

javascript - 如何在AngularJs中通过$stateProvider传递参数?

javascript - 将内置 Angular 指令绑定(bind)到自定义指令中的方法

angularjs - 如何使用 ng-click 从另一个函数调用一个函数?

仅具有私有(private)功能的 AngularJS 单元测试 Controller

angularjs - orderBy number angular js

css - 更改 URL,打开模态窗口时,可以在 url Bootstrap 3/AngularJS/CSS 之后进入模态窗口

css - UI-Router 状态监听器指令