4

I'm trying to build a mechanism which will allow me to resolve directives within a linking function.

Example:

angular.module('directives', [])

    .directive('myContainer', ['$compile', function ($compile) {
            return {
                restrict: "E",
                replace: true,
                link: function (scope, element, attrs) {
                   angular.forEach(scope.component.components, function(component){

                       var newScope = scope.$new()
                       newScope.component = component;
                       var elem = angular.element('<'+component.type+'>'+'</'+component.type+'>')

                       //Trying to compile directive to be resolved
                       var resolvedDirective = $compile(elem)(scope) 
                       element.append(resolvedDirective)
                  })
                }
            });

Problem is, the "resolvedDirective" (defined by component => type) simply creates a tag containing the other directive name, which will be resolved later on.

My mechanism was simplified for the sake of the example (recursive...)

Hope I've made my question clear enough...

Thanks in advance!

lxngxr
  • 127
  • 1
  • 6
  • Your example and the behaviour seems completely fine. I think i didn't get the question. Can you put it into a plunker and describe the behaviour you need with examples? – Umut Benzer Jul 03 '15 at 16:27
  • Can you clarify the question and problem a little bit? I made a jsfiddle with your code and it seems to be working fine. http://jsfiddle.net/nyL441c7/3/ – TwitchBronBron Jul 14 '15 at 13:50

1 Answers1

0

Need more clarity in your Question . Please update with desired result of your directive.

You can follow this pattern. This answer has explained lot about how to add compile directives within directives

Angularjs directive add directives as attribute and bind them dynamically

In your case you want to append new directive , compile and bind it .

So try to add append your element in Compile phase and use $compile in postLink phase.

Community
  • 1
  • 1
Amerrnath
  • 2,227
  • 4
  • 22
  • 33