Dynamically displaying template in ng-repeat directive in AngularJS?

后端 未结 2 1281
北荒
北荒 2020-12-09 05:56

I am attempting to dynamically display one of several templates within an ng-repeat directive, based on the current item.

My JSON data looks like this:



        
2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-09 06:22

    You could do something like this:

    {{ group.name }}

    Then in your controller:

    $scope.getIncludeFile = function(section) {
        // Make this more dynamic, but you get the idea
        switch (section) {
            case "Section A":
                return 'partials/sectiona.html';
            case "Section B":
                return 'partials/sectionb.html';
        }
    }
    

    Then your sectiona.html could look like this (to have a controller specific to that file):

提交回复
热议问题