2014-10-16 68 views
0

我正在使用装饰器扩展第三方指令。我想访问装饰者中的一个工厂。我怎样才能做到这一点?AngularJS:从装饰器中访问工厂

$provide.decorator('multiSelectDirective', function($delegate) { 
    var directive = $delegate[0], 
     link = directive.link; 

    // wipe out the shitty template 
    directive.template = ''; 

    // make with the new template! 
    directive.templateUrl = 'app/partials/filters.template.html'; 

    // hook into the compile phase of the directive 
    directive.compile = function() { 

     // the function returned by compile is the new link function 
     return function($scope, el, attrs) { 

      // run the original link function. 
      link.apply(this, arguments); 

      $scope.filterClicked = function(buttonName, selection) { 
       handleFilterClick(buttonName, selection, JiraData, GreyGooseApi);    
      } 

     } 
    }; 

    return $delegate; 
}); 

回答

0

我想通了。我能够通过以下方式来注入依赖关系:

$provide.decorator('multiSelectDirective', 
    [ '$delegate', 'JiraData', 'GreyGooseAPI', 
    function($delegate, JiraData, GreyGooseApi) { 
     var directive = $delegate[0], 
      link = directive.link; 

     // wipe out the shitty template 
     directive.template = ''; 

     // make with the new template! 
     directive.templateUrl = 'app/partials/filters.template.html'; 

     // hook into the compile phase of the directive 
     directive.compile = function() { 

      // the function returned by compile is the new link function 
      return function($scope, el, attrs) { 

       // run the original link function. 
     link.apply(this, arguments); 

     $scope.filterClicked = function(buttonName, selection) { 
      handleFilterClick(buttonName, selection, JiraData, GreyGooseApi); 
     } 

      } 
     }; 

     return $delegate; 
    }]); 

}); 
0

您可以通过$delegate.someproperty访问所有的工厂公开的属性如果不暴露你不能访问属性。