码迷,mamicode.com
首页 > 其他好文 > 详细

[Angular] Learn How To Use ng-template Inputs

时间:2017-07-02 21:15:08      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:tar   dynamic   base   ops   str   learn   title   blog   amp   

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

[Angular] Learn How To Use ng-template Inputs

标签:tar   dynamic   base   ops   str   learn   title   blog   amp   

原文地址:http://www.cnblogs.com/Answer1215/p/7107032.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!