[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>
[Angular] Change component default template (ng-content, ng-template, ngTemplateOutlet, TemplateRef)的更多相关文章
- [Angular] Test component template
Component: import { Component, Input, ChangeDetectionStrategy, EventEmitter, Output } from '@angular ...
- [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 ...
- [Angular 2] Component relative paths
Oingial aritial --> Link Take away: import { Component, OnInit } from '@angular/core'; @Component ...
- 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 ...
- angular 引入 component 报错
每天抽出一些时间学习 Angular2 ,并准备把手头上的项目移植为 Angular2 , 不过今天又遇到了一些小问题. 准备写一个导航类适于管理后台常见的右边导航,如博客园的这种: ! 使用 g g ...
- 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 ...
- [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 ...
- 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 ...
- MATLAB/SIMULINK生成代码错误之change the default character encoding setting
SIMULINK点击生成C代码报错 错误提示: Error encountered while executing PostCodeGenCommand for model 'RTW_sc3': Cl ...
随机推荐
- 阿里云Redis使用规范
一.键值设计 1.key名设计 (1)[建议]: 可读性和可管理性 以业务名(或数据库名)为前缀(防止key冲突),用冒号分隔,比如业务名:表名:id ugc:video:1 (2)[建议]: 简洁性 ...
- python hmac 加密
python2 : key 是秘钥 类型为 str msg 要加密的文件 str digestmod 要加密的方式 python3: key 是秘钥 类型为 byte msg 要加密的文件 byte ...
- python版 百度签到
经常玩贴吧,刚学python ,所以自己弄了一个python版的签到程序.自己的东西总是最好的. 登陆模块参考的http://www.crifan.com/emulate_login_website_ ...
- 解决xorm逆向mssql报datetime2不兼容的异常错误
xorm作为golang开发者的一大利器,深受大家的喜爱,可是最近在逆向mssql的时候,报了这么一个错误: 最后找了半天发现xorm没有预置DateTime2类型,经过几番折腾,在xorm源码的en ...
- sqlserver 运行正則表達式,调用c# 函数、代码
--1.新建SqlServerExt项目,编写 C# 方法生成 SqlServerExt.dll 文件 using System; using System.Data; using System.Da ...
- bug 7715339 登录失败触发 ‘row cache lock’ 等待
Bug 7715339 - Logon failures causes "row cache lock" waits - Allow disable of logon delay ...
- 关于client浏览器界面文字内容溢出用省略号表示方法
在实际的项目中,因为client浏览器文字内容的长度不确定性和页面布局的固定性,难免会出现文字内容超过div(或其它标签,下同)区域的情况.此时比較好的做法就是当文字超过限定的div宽度后自己主动以省 ...
- 9.使用 npm 命令安装模块
转自:http://www.runoob.com/nodejs/nodejs-tutorial.html npm 安装 Node.js 模块语法格式如下: $ npm install <Modu ...
- 35.Intellij IDEA设置忽略部分类编译错误
转自:https://www.aliyun.com/jiaocheng/290360.html 有些时候我们的项目中有些错误,但这些错误并不影响项目的整体运行(或许是没有使用到),默认情况下idea是 ...
- 读阮一峰《ECMAScript 6 入门》小结
读阮一峰<ECMAScript 6 入门>小结,http://es6.ruanyifeng.com/ 1. ES6简介 Babel 是一个广泛使用的 ES6 转码器,可以将 ES6 代码转 ...