将 resolved 属性 注入抽象父级的控制器

Inject resolved property into controller of abstract parent

我定义了我的 UI-路由器状态,如下所示:

$stateProvider.state('contact', {
        url: '/contactDemo',
        views: {
            'main': {
                controller: 'contactMainController',
                templateUrl: 'templates/contact.tpl.html'
            }
        }
    }).state('contact.details', {
        abstract: true,
        controller: 'contactDetailsController',
        templateUrl: 'templates/contact.details.tpl.html'
    }).state('contact.details.add', {
        url: '/add'
    }).state('contact.details.filter', {
        url: '/filter/{filterId}'
    }).state('contact.details.filter.record', {
        url: '/record/{recordId}',
        resolve: {
            record: ['$stateParams', 'Restangular', function($stateParams, Restangular) {
                return Restangular.one('records', $stateParams.recordId).get();
            }]
        }
    }).state('contact.details.filter.record.edit', {
        url: '/edit'
    });

现在我想将我已解决的 record 注入 contactDetailsController。如果这样做,我会收到 Unknown provider 错误。我无法将 resolve 移动到抽象状态,因为从那里我无法访问 $stateParams.

中的 id

如果我将 controller 属性 向下移动到子状态,则永远不会调用我的控制器。

有谁知道如何将已解析的 属性 注入到抽象父状态的控制器中?

如记录here,resolve 可以被继承。未从 child 注入到 parent.

Inherited Resolved Dependencies

New in version 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;
      }

简单的解决方案,其中:

  1. $stateParam定义在child
  2. 依赖它的resolve应该被注入parent

无法工作 - 因为一个parent将被许多child人共享。

Parent will stay while the children's params will differ..