Search code examples
angularjsangularjs-injector

How to load angularjs directive use $injector


I'm trying to use directive from other modules that is loaded by angular.injector but angular can't understand directives that is in other_module.
In my main file, I declare my module.

  angular.module('blockApp', []).directive('', []);      

In second file, I try to add angularCharts as dependency:

  var otherBlock = angular.module('blockApp');
  otherBlock.run(function() {
      angular.injector(['ng', 'angularCharts']);
  });

Then I use angularCharts's directives but angularjs can't identify them. I know we should add dependent modules when we declare module but it is special case.


Solution

  • here's a plnkr http://plnkr.co/edit/Q5oD65wSvBAemf5BCDL7?p=preview

    In this case you can use $compile (you still need a $scope from the example, but $compile pretty much answers your question I guess),

        var $compile = chartsInjector.get("$compile");
        console.log("$compile=",$compile);
    
        var chartsDirective = $compile("<div charts></div>");
        console.log(chartsDirective);
    

    ...

      var blockapp = angular.module('blockApp', []); 
      blockapp.run(function(){
        // create a new injector
        var chartsInjector = angular.injector(['ng', 'angularCharts']);
        console.log("chartsInjector=",chartsInjector);
    
        var $compile = chartsInjector.get("$compile");
        console.log("$compile=",$compile);
    
        var chartsDirective = $compile("<div charts></div>");
        console.log(chartsDirective);
      })
    

    note that you will be creating multiple injectors here.

    injector 1) created by ng-app

    <body ng-app="blockApp">
    

    injector 2) created by angular.injector()

    var chartsInjector = angular.injector(['ng', 'angularCharts']);
    

    whole code

    <!DOCTYPE html>
    <html>
    
      <head>
        <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
        <script type="text/javascript">
          // creating the angularcharts
          angular.module('angularCharts', []).directive('charts', function(){
            return function(){}
          }); 
    
          // creating blockapp
          angular.module('blockApp', []).directive('', []); 
    
          // retrieving the created blockapp
          // NOTE! ng-app will create a new injector for blockapp 
          var blockapp = angular.module('blockApp', []); 
          blockapp.run(function(){
            // create a new injector
            var chartsInjector = angular.injector(['ng', 'angularCharts']);
            console.log("chartsInjector=",chartsInjector);
    
            var $compile = chartsInjector.get("$compile");
            console.log("$compile=",$compile);
    
            var chartsDirective = $compile("<div charts></div>");
            console.log(chartsDirective);
          })
    
        </script>
      </head>
    
      <body ng-app="blockApp">
    
      </body>
    
    </html>
    

    plnkr http://plnkr.co/edit/Q5oD65wSvBAemf5BCDL7?p=preview