I am trying to dynamically visualize directives based on an array of directive name configurations. Is this possible in angular? I also want these visualized directives to live inside the same parent dom element, and not everyone, getting a new wrapper (as with ng-repeat).
http://jsfiddle.net/7Waxv/
var myApp = angular.module('myApp', []); myApp.directive('one', function() { return { restrict: 'A', template: '<div>Directive one</div>' } }); myApp.directive('two', function() { return { restrict: 'A', template: '<div>Directive two</div>' } }); function MyCtrl($scope) { $scope.directives = ['one', 'two']; } <div ng-controller="MyCtrl"> <div ng-repeat="directive in directives"> <div {{directive}}></div> </div> </div>
EDIT: After posting this question, I also tried:
.directive('parentDirective', function () { return { restrict: 'A', replace: true, link: function (scope, element) { scope.directives = ['one', 'two']; for (var i = 0; i < scope.directives.length; i++) { element.prepend('<div ' + scope.directives[i] + '></div>') } } }; }); <div parent-directive></div>
However, templates from predefined directives are not displayed.
source share