zoukankan      html  css  js  c++  java
  • [Angular] Change component default template (ng-content, ng-template, ngTemplateOutlet, TemplateRef)

    Here is the defulat tab header template:

    <ng-template #defaultTabHeader let-tabs="tabsX">
      <ul class="tab-panel-buttons" *ngIf="tabs">
        <li
          [ngClass]="{selected: tab.selected}"
          (click)="selectTab(tab)"
          *ngFor="let tab of tabs;">{{tab.title}}
        </li>
      </ul>
    </ng-template>
    
    <ng-content
      *ngTemplateOutlet="defaultTabHeader; context: tabsContext"></ng-content>
    
    <ng-content></ng-content>

    we have set up that we can switch the default template when we pass in another template.

    Now, what we want is able to pass in a custom template to replace the default header.

      <ng-template #headerButtons>
        <button>Login</button>
        <button>Sign up</button>
      </ng-template>

    This is the new template, we want to replace the default header.

    Now we pass this to the component:

      <ng-template #headerButtons>
        <button>Login</button>
        <button>Sign up</button>
      </ng-template>
    
        <au-tab-panel [headerTemplate]="headerButtons">

    Create a 'headerTemplate' Input on the component, and pass in the template ref.

    export class AuTabPanelComponent implements OnInit, AfterContentInit {
    
      @Input()
      headerTemplate: TemplateRef<any>;

    Now we need to check whether the custom template passed in or not, if passed in then we use custom template, otherwise the default template.

    <ng-template #defaultTabHeader let-tabs="tabsX">
      <ul class="tab-panel-buttons" *ngIf="tabs">
        <li
          [ngClass]="{selected: tab.selected}"
          (click)="selectTab(tab)"
          *ngFor="let tab of tabs;">{{tab.title}}
        </li>
      </ul>
    </ng-template>
    
    <ng-content
      *ngTemplateOutlet="headerTemplate ? headerTemplate :defaultTabHeader; context: tabsContext"></ng-content>

    Now we need to export the 'au-tab-panel' component's method to the custom template, to do that we can use template ref:

      <ng-template #headerButtons>
        <button (click)="tabPanel.selectTab(loginTab)">Login</button>
        <button (click)="tabPanel.selectTab(signUpTab)">Sign up</button>
      </ng-template>
    
        <au-tab-panel #tabPanel [headerTemplate]="headerButtons">
    
          <au-tab title="login" #loginTab>
            <form>
              <div class="form-field">
                <label>Email:</label><input>
              </div>
              <div class="form-field">
                <label>Password:</label><input>
              </div>
            </form>
          </au-tab>
    
          <au-tab title="Sign up" #signUpTab>
            <form>
              <div class="form-field">
                <label>Email:</label><input>
              </div>
              <div class="form-field">
                <label>Password:</label><input>
              </div>
              <div class="form-field">
                <label>Confirm Password:</label><input>
              </div>
            </form>
          </au-tab>
  • 相关阅读:
    一套完整的javascript面试题
    遇到的java.lang.NoClassDefFoundError解决了
    Win7下启动Internet信息服务(IIS)管理器
    我的第一个专业博客
    “用NetBeans打开项目时项目名变成红色”问题解决
    Struts2框架实现计算器功能
    MyEclipse移动包到另一个项目时出现错误:Resource is out of sync with the file system.
    制作Javascript弹出窗口技巧九则
    windows 的鼠标事件(Event)
    Javascript使用cookie
  • 原文地址:https://www.cnblogs.com/Answer1215/p/7052147.html
Copyright © 2011-2022 走看看