Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practices: Should I use ng-switch for this? [closed]

I've got this object in Angular.

$scope.columns = {
    workspace: {
        title: "Workspace",
        type: "workspace",
        activities: []
    },
    alerts: {
        title: "Alerts",
        type: "alert",
        activities: []
    },
    main: {
        title: "Main Feed",
        type: "main",
        activities: []
    }
};

And in my HTML I need to loop through it to dynamically create a series of columns in my app (think something like Trello)

Each type is a reference to a custom directive.

I'm trying to figure out the best way to place my directives.

Based on this data, is the code below an appropriate way to handle dynamically creating these?

<div ng-repeat="(key, obj) in columns">

    <div ng-switch on="obj.type">
        <workspace-feed ng-switch-when="workspace" />
        <alert-feed ng-switch-when="alert" />
        <main-feed ng-switch-when="main" />
        <filter-feed ng-switch-when="filter" />
    </div>

</div>

I'd love to be able to do something like... <{{obj.type}}-feed /> but I'm not sure if this is valid, or if there's a better way to create this.

Thoughts are much appreciated!

like image 905
Chris R. Avatar asked Jul 07 '15 20:07

Chris R.


1 Answers

What you have so far looks fine.

Depending on how different your columns are, you may opt to use just one directive which dynamically loads a template instead of multiple directives. For example, take a look at ng-include:

<div ng-include="'columns/' + column.type + '.html'"></div>
like image 186
Agop Avatar answered Oct 28 '22 06:10

Agop