在angular中使用html模板's ng开关

Using html templates in angular's ng-switch

本文关键字:ng 开关 模板 angular html      更新时间:2023-09-26

我正在制作一个随着用户点击而移动的"交互式菜单"。我想知道是否有一种方法可以在ng switch中包含html模板,由于每个"开关"中的所有逻辑都不同,因此会产生巨大的html文件。

<div class="content" ng-switch on="selection">
 <div ng-switch-when="1" >
   <h1>1</h1>
 </div>
 <div ng-switch-when="2">
   <h1>2</h1>       
 </div>
</div>

使用ngInclude:

<div class="content" ng-switch on="selection">
    <div ng-switch-when="1" >
        <ng-include src="'template1.html'"></ng-include>
    </div>
    <div ng-switch-when="2">
        <ng-include src="'template2.html'"></ng-include>
    </div>
</div>

注意:如果你正在硬编码路径,不要忘记使用双引号中的单引号。

您应该能够使用ng-include指令:

<div class="content" ng-switch on="selection">
    <ng-switch-when="1" ng-include="//TEMPLATE PATH">
    <ng-switch-when="2" ng-include="//TEMPLATE 2 PATH">
</div> 
   **I used ng-Include this way.**
    <!-- Main content -->
    <div class="row">
      <!-- right col -->
      <section class="col-lg-12">
        <ul class="nav nav-tabs responsive ui-tabbed" id="myTab">
          <li class="active">
            <a  data-ng-click=' main.active.tab = "tab-1" '>Tab 1</a>
          </li>
    </ul>
    <!-- end responsive tabs -->
    <div class="tab-content ui-tabbed-contents responsive">
    <div data-ng-switch = " main.active.tab ">
      <div data-ng-switch-when='tab-1' >
        <ng-include src="'tab-one.html'" ></ng-include>
      </div>
    </div>
    </div>

    </div>
    <!-- end main tabs container -->
    </section>