在同一页面/选项卡中显示2个模板

时间:2016-02-15 17:05:05

标签: javascript html angularjs angularjs-ng-template

我的ng-template有问题。 观看this example,如果我添加下拉菜单另一个选项(ex template3.html),我该如何一起显示第一个和第二个模板?

HTML:

  <!-- template1.html -->
  <script type="text/ng-template" id="template1.html">
    Content of template1.html
  </script>

  <!-- template2.html -->
  <script type="text/ng-template" id="template2.html">
    Content of template2.html
  </script>


  <div ng-controller="Ctrl">
    <select ng-model="template" ng-options="t.name for t in templates">
    </select>
    url of the template: <tt>{{template.url}}</tt>
    <hr/>
    <div ng-include src="template.url"></div>
  </div>    

JS:

function Ctrl($scope) {
  $scope.templates =
    [ { name: 'template1.html', url: 'template1.html'}
    , { name: 'template2.html', url: 'template2.html'}
    , { name: 'template3.html', url: }//this must contain first and second template
    ];
  $scope.template = $scope.templates[0];
}

1 个答案:

答案 0 :(得分:1)

创建包含其他两个

的模板
  <script type="text/ng-template" id="template3.html">
     <div ng-include src="'template1.html'"></div>
     <div ng-include src="'template2.html'"></div>
  </script>

JS

function Ctrl($scope) {
  $scope.templates =
    [ { name: 'template1.html', url: 'template1.html'}
    , { name: 'template2.html', url: 'template2.html'}
    , { name: 'template3.html', url: 'template3.html' }
    ];
  $scope.template = $scope.templates[0];
}

DEMO