Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have a very boiled down version of what I am doing that gets the problem across.

I have a simple directive. Whenever you click an element, it adds another one. However, it needs to be compiled first in order to render it correctly.

My research led me to $compile. But all the examples use a complicated structure that I don't really know how to apply here.

Fiddles are here: http://jsfiddle.net/paulocoelho/fBjbP/1/

And the JS is here:

var module = angular.module('testApp', [])
    .directive('test', function () {
    return {
        restrict: 'E',
        template: '<p>{{text}}</p>',
        scope: {
            text: '@text'
        },
        link:function(scope,element){
            $( element ).click(function(){
                // TODO: This does not do what it's supposed to :(
                $(this).parent().append("<test text='n'></test>");
            });
        }
    };
});

Solution by Josh David Miller: http://jsfiddle.net/paulocoelho/fBjbP/2/

share|improve this question
up vote 219 down vote accepted

You have a lot of pointless jQuery in there, but the $compile service is actually super simple in this case:

.directive( 'test', function ( $compile ) {
  return {
    restrict: 'E',
    scope: { text: '@' },
    template: '<p ng-click="add()">{{text}}</p>',
    controller: function ( $scope, $element ) {
      $scope.add = function () {
        var el = $compile( "<test text='n'></test>" )( $scope );
        $element.parent().append( el );
      };
    }
  };
});

You'll notice I refactored your directive too in order to follow some best practices. Let me know if you have questions about any of those.

share|improve this answer
29  
Awesome. It works. See, these simple and basic examples are the ones that should be shown in angulars' docs. They start off with complicated examples. – PCoelho Mar 7 '13 at 20:48
1  
Thanks, Josh, this was really useful. I made a tool in Plnkr that we are using in a new CoderDojo to help kids learn how to code, and I just extended it so that I can now use Angular Bootstrap directives like datepicker, alert, tabs, etc. Apparently I msssed something up and right now it's only working in Chrome though: embed.plnkr.co/WI16H7Rsa5adejXSmyNj/preview – JoshGough Jun 30 '13 at 19:31
3  
Josh - what's an easier way to accomplish this without using $compile? Thanks for your answer by the way! – doubleswirve Jan 28 '14 at 0:25
2  
@doubleswirve In this case, it would be far easier to just use ngRepeat. :-) But I assume you mean adding new directives dynamically to the page, in which case the answer is no - there's no simpler way because the $compile service is what wires directives up and hooks them into the event cycle. There's no way around $compileing in a situation like this, but in most cases another directive like ngRepeat can accomplish the same job (so ngRepeat is doing the compiling for us). Do you have a specific use case? – Josh David Miller Jan 29 '14 at 19:09
1  
Shouldn't the compile happen in the prelink stage? I think that the controller should only contain non-DOM, unit-testable code, but I'm new to the link/controller concept so I'm unsure myself. Also, one basic alternative is ng-include + partial + ng-controller since it will act as a directive with inherited scope. – Marcus Nielsen Oct 7 '14 at 15:15

In addition to perfect Riceball LEE's example of adding a new element-directive

newElement = $compile("<div my-directive='n'></div>")($scope)
$element.parent().append(newElement)

Adding a new attribute-directive to existed element could be done using this way:

Let's say you wish to add on-the-fly my-directive to the span element.

template: '<div>Hello <span>World</span></div>'

link: ($scope, $element, $attrs) ->

  span = $element.find('span').clone()
  span.attr('my-directive', 'my-directive')
  span = $compile(span)($scope)
  $element.find('span').replaceWith span

Hope that helps.

share|improve this answer
    
This is exactly what I was looking for, thanks! – Erin Drummond Jan 30 '14 at 3:23
2  
Don't forget to remove the original directive in order to prevent Maximum call stack size exceeded error. – SRachamim Mar 26 '14 at 13:27
    
Hi, would you please provide ideas on my new proposed API to make programmatically adding directives a simpler process? github.com/angular/angular.js/issues/6950 Thanks! – trusktr Apr 5 '14 at 4:14
    
I wish in 2015 we wouldn't have limits in call stack size. :( – psycho brm Jul 31 '15 at 13:57
3  
The Maximum call stack size exceeded error always happens because of infinite recursion. I've never seen an instance where increasing the stack size would solve it. – Gunchars Jul 31 '15 at 18:13

Dynamically adding directives on angularjs has two styles:

Add an angularjs directive into another directive

  • inserting a new element(directive)
  • inserting a new attribute(directive) to element

inserting a new element(directive)

it's simple. And u can use in "link" or "compile".

var newElement = $compile( "<div my-diretive='n'></div>" )( $scope );
$element.parent().append( newElement );

inserting a new attribute to element

It's hard, and make me headache within two days.

Using "$compile" will raise critical recursive error!! Maybe it should ignore the current directive when re-compiling element.

$element.$set("myDirective", "expression");
var newElement = $compile( $element )( $scope ); // critical recursive error.
var newElement = angular.copy(element);          // the same error too.
$element.replaceWith( newElement );

So, I have to find a way to call the directive "link" function. It's very hard to get the useful methods which are hidden deeply inside closures.

compile: (tElement, tAttrs, transclude) ->
   links = []
   myDirectiveLink = $injector.get('myDirective'+'Directive')[0] #this is the way
   links.push myDirectiveLink
   myAnotherDirectiveLink = ($scope, $element, attrs) ->
       #....
   links.push myAnotherDirectiveLink
   return (scope, elm, attrs, ctrl) ->
       for link in links
           link(scope, elm, attrs, ctrl)       

Now, It's work well.

share|improve this answer
1  
Would love to see a demo of inserting a new attribute to element, in vanilla JS if possible - I'm missing something... – Patrick Nov 21 '13 at 12:08
    
the real example of inserting a new attribute to element is here(see my github): github.com/snowyu/angular-reactable/blob/master/src/… – Riceball LEE Dec 6 '13 at 8:45
1  
Doesn't help honestly. This is how I ended up solving my problem though: stackoverflow.com/a/20137542/1455709 – Patrick Dec 6 '13 at 10:52
    
Yes, this case is the inserting an attribute directive into another directive, not the inserting element in template. – Riceball LEE Dec 8 '13 at 3:03
    
What's the reasoning behind doing it outside of the template? – Patrick Dec 8 '13 at 5:28
function addAttr(scope, el, attrName, attrValue) {
  el.replaceWith($compile(el.clone().attr(attrName, attrValue))(scope));
}
share|improve this answer

The accepted answer by Josh David Miller works great if you are trying to dynamically add a directive that uses an inline template. However if your directive takes advantage of templateUrl his answer will not work. Here is what worked for me:

.directive('helperModal', [, "$compile", "$timeout", function ($compile, $timeout) {
    return {
        restrict: 'E',
        replace: true,
        scope: {}, 
        templateUrl: "app/views/modal.html",
        link: function (scope, element, attrs) {
            scope.modalTitle = attrs.modaltitle;
            scope.modalContentDirective = attrs.modalcontentdirective;
        },
        controller: function ($scope, $element, $attrs) {
            if ($attrs.modalcontentdirective != undefined && $attrs.modalcontentdirective != '') {
                var el = $compile($attrs.modalcontentdirective)($scope);
                $timeout(function () {
                    $scope.$digest();
                    $element.find('.modal-body').append(el);
                }, 0);
            }
        }
    }
}]);
share|improve this answer

Inspired from many of the previous answers I have came up with the following "stroman" directive that will replace itself with any other directives.

app.directive('stroman', function($compile) {
  return {
    link: function(scope, el, attrName) {
      var newElem = angular.element('<div></div>');
      // Copying all of the attributes
      for (let prop in attrName.$attr) {
        newElem.attr(prop, attrName[prop]);
      }
      el.replaceWith($compile(newElem)(scope)); // Replacing
    }
  };
});

Important: Register the directives that you want to use with restrict: 'C'. Like this:

app.directive('my-directive', function() {
  return {
    restrict: 'C',
    template: 'Hi there',
  };
});

You can use like this:

<stroman class="my-directive other-class" randomProperty="8"></stroman>

To get this:

<div class="my-directive other-class" randomProperty="8">Hi there</div>

Protip. If you don't want to use directives based on classes then you can change '<div></div>' to something what you like. E.g. have a fixed attribute that contains the name of the desired directive instead of class.

share|improve this answer
    
Similar problem i am facing, Can you help me here stackoverflow.com/questions/38821980/… – pandu das Aug 11 '16 at 7:08

Josh David Miller is correct.

PCoelho, In case you're wondering what $compile does behind the scenes and how HTML output is generated from the directive, please take a look below

The $compile service compiles the fragment of HTML("< test text='n' >< / test >") that includes the directive("test" as an element) and produces a function. This function can then be executed with a scope to get the "HTML output from a directive".

var compileFunction = $compile("< test text='n' > < / test >");
var HtmlOutputFromDirective = compileFunction($scope);

More details with full code samples here: http://www.learn-angularjs-apps-projects.com/AngularJs/dynamically-add-directives-in-angularjs

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.