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>

[Angular] Change component default template (ng-content, ng-template, ngTemplateOutlet, TemplateRef)的更多相关文章

  1. [Angular] Test component template

    Component: import { Component, Input, ChangeDetectionStrategy, EventEmitter, Output } from '@angular ...

  2. [Angular 2] Share Template Content In Another Template With Content Projection <ng-content>

    Angular 1 provided a mechanism to place content from your template inside of another template called ...

  3. [Angular 2] Component relative paths

    Oingial aritial --> Link Take away: import { Component, OnInit } from '@angular/core'; @Component ...

  4. Change the default MySQL data directory with SELinux enabled

    转载:https://rmohan.com/?p=4605 Change the default MySQL data directory with SELinux enabled This is a ...

  5. angular 引入 component 报错

    每天抽出一些时间学习 Angular2 ,并准备把手头上的项目移植为 Angular2 , 不过今天又遇到了一些小问题. 准备写一个导航类适于管理后台常见的右边导航,如博客园的这种: ! 使用 g g ...

  6. How to change from default to alternative Python version on Debian Linux

    https://linuxconfig.org/how-to-change-from-default-to-alternative-python-version-on-debian-linux You ...

  7. [vue-router] Failed to resolve async component default: Error: Loading chunk 0 failed.

    在整合laravel5.4 和vue2.1的时候遇到一个奇怪的问题 Uncaught SyntaxError: Unexpected token < Error: Loading chunk 0 ...

  8. Vue-cli3.x在开发环境中(router采用 history模式)出现Failed to resolve async component default: Error: Loading chunk {/d} failed.或者Uncaught SyntaxError: Unexpected token <错误

    使用Vue-cli3.x开发环境中(router采用 history模式)出现Failed to resolve async component default: Error: Loading chu ...

  9. MATLAB/SIMULINK生成代码错误之change the default character encoding setting

    SIMULINK点击生成C代码报错 错误提示: Error encountered while executing PostCodeGenCommand for model 'RTW_sc3': Cl ...

随机推荐

  1. es7 --- 新特性

    ES7只有2个特性: includes() 指数操作符 不使用ES7 使用indexOf()验证数组中是否存在某个元素,这时需要根据返回值是否为-1来判断: let arr = ['react', ' ...

  2. c# List集合学习

    1---集合,可以理解成容器 泛型集合 非泛型集合2---使用集合用到的命名空间 using System.Collections.Generic;3---集合是如何来的?集合的前辈是数组,数组在内存 ...

  3. HUD——T 3836 Equivalent Sets

    http://acm.hdu.edu.cn/showproblem.php?pid=3836 Time Limit: 12000/4000 MS (Java/Others)    Memory Lim ...

  4. 关于oracle sql语句查询时 表名和字段名要加双引號的问题具体解释

      作为oracle的刚開始学习的人相信大家一定会遇到这个问题.如图:          明明就是navicat可视化创建了表,但是就是不能查到!这个为什么呢?           select * ...

  5. D. Dreamoon and Sets(Codeforces Round #272)

    D. Dreamoon and Sets time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. Hibernate中编程式事物的简单使用

    一,openSessioin方式开启或者关闭事物 Session session = null; try { session = HibernateUtils.getSession(); sessio ...

  7. Day4晚笔记

    数据结构 并查集:捆绑两个点的信息,判断对错 倍增:LCA, 字符串 hash,模拟, 最小表示法 给定一个环状字符串,切开,使得字符串的字典序最小 图和树 割点,割边,强联通分量 点双联通分量 (把 ...

  8. Non-resolvable parent POM for **: Could not find artifact **

    注意查看这句: 原因是本地仓库缺少了easybuy-parent:pom:0.0.1-SNAPSHOT, 原来是忘记了将父工程打包到本地仓库 ,运行聚合工程前记得先将依赖的工程都先Maven inst ...

  9. Eclipse工具修理集

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 俗话说:工欲善其事.必先利其器,eclipse偶尔会发发脾气,那我们要记录下"她" ...

  10. 前端面试题(webpack)

    (前端面试题大全,持续更新) webpack3升级到4为什么会提升速度? webpack优化有哪些? webpack的css-loader原理讲一下 webpack压缩js css的方法