Angular UI-Router传递解析为child onEnter

时间:2014-09-03 02:32:31

标签: angularjs angular-ui-bootstrap angular-ui-router

我正在尝试将先前解析的结果从父级传递给实例化的onEnter模式实例,但是控制器会报告它是未知的提供者:

Error: [$injector:unpr] Unknown provider: resolvedRouteProvider <- resolvedRoute

是否有其他方法可以注入此值,还是仅限于从onEnter函数调用服务?

作为参考,当导航到类似下面的孩子的状态时会发生错误:

$stateProvider
  .state('parent',
    resolve:
      resolvedRoute: (Restangular) -> Restangular.all('route')
  )
  .state('child',
    parent: 'parent',
    onEnter: ($modal) ->
      $modal.open(
        templateUrl: 'templates/modal.html',
        controller: 'ModalCtrl'
      )
  )

此时控制器非常简单:

.controller('ModalCtrl', ($scope, $modalInstance, resolvedRoute) ->
  $scope.object = {}
  $scope.createObject = ->
    resolvedRoute.all('objects').post({object: $scope.object})
)

Plunker with failing resolve

2 个答案:

答案 0 :(得分:4)

事实证明,你只需要将另一个决心传递给模态,例如

$stateProvider
  .state('parent',
    resolve:
      parentResolve: (Restangular) -> Restangular.all('route')
  )
  .state('child',
    parent: 'parent'
    url: '/child'
    resolve:
      childResolve: (Restangular) -> Restangular.one('bar')
    onEnter: ($modal) ->
      $modal.open(
        templateUrl: 'templates/modal.html',
        controller: 'ModalCtrl'
        resolve:
          parentResolve: -> parentResolve
          childResolve: -> childResolve
      )
  )

ModalCtrl现在可以访问parentResolvechildResolve

请注意:必须从函数返回结果,因此只需解析相同的对象将无法正常工作

resolve:
  parentResolve: parentResolve

答案 1 :(得分:0)

查看此示例,其中显示了如何将已解析的数据注入onEnter函数 https://github.com/angular-ui/ui-router/wiki

像这样:

$stateProvider.state("contacts", {
  template: '<h1>{{title}}</h1>',
  resolve: { title: 'My Contacts' },
  controller: function($scope, title){
    $scope.title = 'My Contacts';
  },
  onEnter: function(title){
    if(title){ ... do something ... }
  },
  onExit: function(title){
    if(title){ ... do something ... }
  }
})