javascripthtmlangularjsangularjs-ng-template

Show 2 template in the same page/tab


I have a problem with ng-template. Watching this example, if I add in dropdown menù another option (ex template3.html), how can I show the first and the second template together?

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];
}

Solution

  • Create a template that includes both of the others

      <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