在AngularJS中的ng-repeat指令中动态显示模板?

时间:2013-04-22 19:51:49

标签: angularjs

我试图根据当前项目动态显示ng-repeat指令中的几个模板之一。

我的JSON数据如下所示:

data: {
    groups: [
        {
            name: "Group 1",                
            sections: [
                { name: "Section A" },
                { name: "Section B" }
            ]
        },
        {
            name: "Group 2",                
            sections: [
                { name: "Section A" },
                { name: "Section B" }
            ]
        }
    ]
}

我的目标是动态呈现数据树,每个组包含多个部分。这些组都将具有相同的模板,但每个部分都应该有自己的模板,基于名称字段。

假设顶级HTML是:

<div ng-repeat="group in groups">
    {{ group.name }}
    <div ng-repeat="section in sections">
        <!-- Dynamic section template used -->
    </div>
</div>

理想情况下,每个部分还需要有自己的范围数据和与之关联的控制器。我有幸用Knockout构建这种类型的系统,但我正在尝试理解Angular的做事方式。

2 个答案:

答案 0 :(得分:33)

你可以这样做:

<div ng-repeat="group in groups">
    {{ group.name }}
    <div ng-repeat="section in sections" ng-include="getIncludeFile(section)">
        <!-- Dynamic section template used -->
    </div>
</div>

然后在你的控制器中:

$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';
    }
}

然后你的sectiona.html看起来像这样(要有一个特定于该文件的控制器):

<div ng-controller="SectionAController">
    <!-- HTML in here, and can bind straight to your SectionAController -->
</div>

答案 1 :(得分:4)

在过去的一个月里,有一个签到角,以支持指令中的动态模板,但我无法找到有关其使用的非常多的信息。这是参考。 https://github.com/angular/angular.js/pull/1849

虽然这仍然使用相同的nginclude,但它全部封装在两个指令中:

演示:http://plnkr.co/edit/4DIlHMNlHQ8Wm9XHNycH?p=preview

HTML:

<groups-control groupdata="groups"></groups-control>

控制器:

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
  var json = {data: {
    groups: [
        {
            name: "Group 1",                
            sections: [
                { name: "Section A" },
                { name: "Section B" }
            ]
        },
        {
            name: "Group 2",                
            sections: [
                { name: "Section A" },
                { name: "Section B" }
            ]
        }
    ]
  }};
  $scope.groups = json.data.groups;

}); 

指令分为两部分:

app.directive('groupsControl', function(){
    return {
      restrict: 'E',

      replace: true,
      transclude: false,
      scope: { items:'=groupdata'},

      template: '<div ng-repeat="group in items">' +
                  '{{ group.name }}' +
                  '<section-control sections="group.sections" />'+

              '</div>',
      // The linking function will add behavior to the template
      link: function(scope, element, attrs) {


      }
    }
  }).directive('sectionControl', function(){
    return {
      restrict: 'E',

      replace: true,
      transclude: false,
      scope: { items:'=sections'},

      template: '<div ng-repeat="section in items" ng-include="getIncludeFile(section)">'+
                '</div>',

      link: function(scope, element, attrs) {
        scope.getIncludeFile = function(section) {
            return section.name.toLowerCase().replace('section ','') + ".html";
        }

      }
    }
  });

我实际上希望看到有人根据某些范围数据使用templateUrl的函数发布答案。