zoukankan      html  css  js  c++  java
  • [Angular] Learn How To Use ng-template Inputs

    For example, we have a modal component, it can config that using ng-template as a configurable template. 

    <ng-template #auModalBody></ng-template>
    
    <au-modal [body]="auModalBody" ></au-modal>

    Now what we want to do is to pass in inputs to ng-templates so that template can change dynamiclly according to the inputs.

    For example:

    <ng-template #auModalBody
          let-title="title"  <!-- define a title prop -->
          let-tabActivated=loginTabActivated <!-- define a tabActivated prop -->
    >
       <!-- we can use 'title' & 'tabActivated' props here -->
       <h2>{{title}}</h2>
       <tab [selected]="tabActivated" #login />
       <tab [selected]="!tabActivated" #signUp />
    </ng-template>
    
    <au-modal [body]="auModalBody" [context]="{
      title: 'Login or Sign up',
      tabActivated: loginTabActivated  <!-- based on those variables we passed in-->
    }"></au-modal>    

    To do that, we need to add an @Input to au-modal component:

    @Input() context: any;
    
    
        <ng-container *ngIf="body; else projectionBody">
          <ng-container *ngTemplateOutlet="body; context:context" ></ng-container>
        </ng-container>

    See the commit: Github

  • 相关阅读:
    归并排序法实现
    HuffmanTree
    快速排序法的实现
    随机数发生器说明
    装箱问题改进
    瞬间判断2的幂
    weblogic删除域
    【转】Mock方法介绍
    Oracle 11G空表无法导出处理
    深入理解JVM虚拟机(一):JVM运行时数据区
  • 原文地址:https://www.cnblogs.com/Answer1215/p/7107032.html
Copyright © 2011-2022 走看看