In my project, I have a small div that has 3 options: "food", "drinks", "social". They are bound to the scope variable "$ scope.option" in the "AppController".
I have a number of ng-switch statements:
<div ng-switch on="option"> <div ng-switch-when="food"> <fooddirective></fooddirective> </div> <div ng-switch-when="drinks"> <drinksdirective></drinksdirective> </div> <div ng-switch-when="social"> <socialdirective></socialdirective> </div> </div>
Please note that regardless of the selected option, food, drink or social, they occupy half the page, so they are all surrounded by the "AppController". My intention is to be able to "dynamically load directives" on the page. Is there a way to get rid of the need to explicitly write "<socialdirective></socialdirective>" or even get rid of the whole ng switch? Or is there some better alternative? It seems like it can get very dirty if I say 20 or 30 of these options (e.g. food, drinks, social games).
If I know the name of the directive in advance, I will say "food", "drinks", "social". Is there a way to do something like:
<div ng-switch on="option"> // code that interprets option variable (ie food), appends the text "directive" after, and dynamically/lazily add it in and remove it so it behaves like an ng-switch </div>
I'm not sure if this is possible, but I am looking for a better alternative to what I am doing now. Any examples of a revised way of doing is great.
source share