Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs - Using directive to instantiate other directives?

So lets say in my HTML I have something like this:

<tabcontent></tabcontent>

Then the javascript for this directive is this:

tabsApp.directive('tabcontent', function(){

  var myObj = {
    priority:0,
    template:'<div></div>',
    replace: true,
    controller: 'TabCtrl',
    transclude: false,
    restrict: 'E',
    scope: false,
    compile: function (element, attrs){
      return function (parentScope, instanceEle){
        parentScope.$watch('type', function(val) {
          element.html('<div '+val+'></div>');
        });
      }
      $compile(parentScope);
    },
    link: function postLink(scope, iElement, iAttrs){}
  };
  return myObj;

});

The HTML is parsed properly, and the value for type is found in the controller JS.

so <tabcontent></tabcontent> is replaced with <div recipe></div> for example..

(that part does happen properly)

So I also have a directive for recipe:

tabsApp.directive('recipe', function(){

  var myObj = {
    priority:0,
    template:'<div>TESTING</div>',
    replace: true,
    controller: 'TabCtrl',
    transclude: false,
    restrict: 'E',
    scope: false,
    compile: function (element, attrs){
      return {
        pre: function preLink(scope, iElement, iAttrs, controller){},
        post: function postLink(scope, iElement, iAttrs, controller){}
      }
    },
    link: function postLink(scope, iElement, iAttrs){}
  };
  return myObj;

});

Which is obviously pretty simple, and just for testing. But the recipe directive is not being processed...

Whats going on here?

like image 413
Elliot Avatar asked Jan 28 '13 13:01

Elliot


People also ask

Can we use multiple directives in AngularJS?

... is quite illustrative as AngularJS doesn't allow multiple directives (on the same DOM level) to create their own isolate scopes. According to the documentation, this restriction is imposed in order to prevent collision or unsupported configuration of the $scope objects.

Which directive do we use to inform AngularJS about the parts controlled by it?

The ngRef attribute tells AngularJS to assign the controller of a component (or a directive) to the given property in the current scope. It is also possible to add the jqlite-wrapped DOM element to the scope. The ngRepeat directive instantiates a template once per item from a collection.

Which directive initializes an AngularJS application?

The ng-app directive initializes an AngularJS application. The ng-init directive initializes application data. The ng-model directive binds the value of HTML controls (input, select, textarea) to application data.

What is two way binding in AngularJS?

Two-way Binding Data binding in AngularJS is the synchronization between the model and the view. When data in the model changes, the view reflects the change, and when data in the view changes, the model is updated as well.


1 Answers

You need to change 2 things:

  1. The recipe directive must not be restricted to E (element). If you are generating the directive like <div recipe></div>, you must at least add A (attribute) to the restrict property on the directive configuration:

    app.directive('recipe', function() {
       return {
          restrict: 'E',
          ...
    
  2. You need to compile the HTML content of the tabcontent directive after the 'watch':

    app.directive('tabcontent', function($compile){
       return {    
       ...    
       link: function (scope, iElement, iAttrs) {
                scope.$watch('type', function(val) {
                   iElement.html('<div '+val+'></div>');           
                   $compile(iElement.contents())(scope);         
                 });
             }
       ...        
    

jsFiddle: http://jsfiddle.net/bmleite/n2BXp/

like image 116
bmleite Avatar answered Nov 03 '22 00:11

bmleite