Injecting resolves into directive controllers

后端 未结 2 1966
栀梦
栀梦 2021-02-07 23:50

I\'m using AngularUI router (0.2.13) and have a state defined as such

.state(\'foo\', { 
           template:\'
\',
2条回答
  •  庸人自扰
    2021-02-08 00:24

    You can't use resolves in directives, but you can pass the result resolved in the state down to the directive which I think accomplishes what you're looking for.

    You'd want to update your state definition to include a controller and set a parameter on the directive:

    .state('foo', { 
       template:'Test
    ', url: 'foo', resolve:{ foo:function(SomeService){ return SomeService.something(); } }, controller: function($scope, foo){ $scope.foo = foo; } })

    Then update the directive to use this parameter:

    .directive('someDirective', function(){
         return {
             controller: function($scope) {
               // I want `data` to be injected from the resolve... 
               // as it would if this was a "standalone" controller
               console.log('$scope.something: '+ $scope.something);
             },
             scope: {
               something: '='
             }
         };
    })
    

    Here's a sample plunker: http://plnkr.co/edit/TOPMLUXc7GhXTeYL0IFj?p=preview

提交回复
热议问题