0

I have a number of directives in my angular module, these are named: MyDirective1, MyDirective2, ... , MyDirectiveN

I maintain a list of directive names as well: MyDirectiveList: ['MyDirective1', 'MyDirective2', ... , 'MyDirectiveN']

In HTML, I would like to display one of the directives based a variable (selectedDirective) which holds the directive name (either with ng-if or ng-show).

<div ng-repeat="directiveName in MyDirectiveList">

   <div ng-if="selectedDirective == directiveName">

   ...directive shall go in here...

   </div>

</div>

I could insert into the element using $injector and $compile in the controller but I wonder if there is more angular way of doing this directly in the html with angular directives.

1 Answer 1

0

Since you are manipulating the DOM, you should do that with a directive.

Well, you may reply that ng-repeat and ng-if are directives as well, and you are right. But you are just currently calling an army because someone stole your wallet.

The best solution seems to be the one presented here, ie a directive to rule them all.

This solution is better for two reasons :

1) You are no longer theorically able to create and not display one thousand DOM object just for displaying one.

2) You can pass a custom json of data if your directives needs to (note that you can only pass it through a unique json and not adapt to the needs of you directives, ie multiple fields with smart two way data-biding).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.