最佳实践:我应该使用ng-switch吗?

Chr*_* R. 6 javascript angularjs ng-switch

我在Angular中有这个对象.

$scope.columns = {
    workspace: {
        title: "Workspace",
        type: "workspace",
        activities: []
    },
    alerts: {
        title: "Alerts",
        type: "alert",
        activities: []
    },
    main: {
        title: "Main Feed",
        type: "main",
        activities: []
    }
};
Run Code Online (Sandbox Code Playgroud)

在我的HTML中,我需要循环它以在我的应用程序中动态创建一系列列(想想像Trello)

每个type都是对自定义指令的引用.

我正试图找出放置指令的最佳方法.

基于这些数据,下面的代码是否适合处理动态创建这些?

<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>
Run Code Online (Sandbox Code Playgroud)

我希望能够做类似...... <{{obj.type}}-feed />但我不确定这是否有效,或者是否有更好的方法来创建它.

非常感谢您的想法!

Ago*_*gop 2

到目前为止你所拥有的看起来不错。

根据您的列的不同程度,您可以选择仅使用一个动态加载模板的指令,而不是使用多个指令。例如,看一下ng-include

<div ng-include="'columns/' + column.type + '.html'"></div>
Run Code Online (Sandbox Code Playgroud)