[Angular] Angular Advanced Features - ng-template , ng-container, ngTemplateOutlet
Previously we have tab-panel template defined like this:
<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-content></ng-content>
So the template is not overrideable. If we want later able to pass in a different template, we need to use some advanced features from Angular.
ng-template: We can wrap the whole header into <ng-template>, by defualt, ng-template will not render to the DOM.
<ng-template #defaultTabHeader>
<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>
To be able to render the template to the DOM; we need to use <ng-content>:
<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>
import {AfterContentInit, Component, ContentChildren, OnInit, QueryList} from '@angular/core';
import {AuTabComponent} from '../au-tab/au-tab.component'; @Component({
selector: 'au-tab-panel',
templateUrl: './au-tab-panel.component.html',
styleUrls: ['../tab-panel.component.scss']
})
export class AuTabPanelComponent implements OnInit, AfterContentInit { @ContentChildren(AuTabComponent)
tabs: QueryList<AuTabComponent>; constructor() { } ngOnInit() {
} ngAfterContentInit(): void {
const selectedTab = this.tabs.find(tab => tab.selected);
if(!selectedTab && this.tabs.first) {
this.tabs.first.selected = true;
}
} selectTab(tab: AuTabComponent) {
this.tabs.forEach(t => t.selected = false);
tab.selected = true;
} get tabsContext() {
return {
tabsX: this.tabs
};
} }
[Angular] Angular Advanced Features - ng-template , ng-container, ngTemplateOutlet的更多相关文章
- [Angular 6] 初学angular,环境全部最新,[ ng serve ] 不能启动,卡在 95% 不动 => 解决方案
2018.9.7 问题描述: 通过ng serve命令启动angular应用时,卡在95%, ctrl+c 停掉后看到错误内容为找不到ng_modules下的angular模块下的package.js ...
- [Python] Advanced features
Slicing 12345 L[:10:2] # [0, 2, 4, 6, 8]L[::5] # 所有数,每5个取一个# [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, ...
- Angular template ng-template/container/content
1. ng-template 形式:<ng-template>...</ng-template> 默认ng-template中的内容会隐藏; 可通过[ngIf]来控制内容显示隐 ...
- [Angular] Change component default template (ng-content, ng-template, ngTemplateOutlet, TemplateRef)
Here is the defulat tab header template: <ng-template #defaultTabHeader let-tabs="tabsX" ...
- [Angular Directive] Structure directive and <template>
The structure directive is just a sugar syntax of <template>. Such as: <div *ngIf="nam ...
- [Angular] Angular Elements Intro
Make sure install the latest Angular v6 with Angular CLI. Checkout ght Github for the code. 1. Creat ...
- Angular - - angular.bind、angular.bootstrap、angular.copy
angular.bind 返回一个调用self的函数fn(self代表fn里的this).可以给fn提供参数args(*).这个功能也被称为局部操作,以区别功能. 格式:angular.bind(se ...
- Angular - - angular.element
angular.element 将DOM元素或者HTML字符串一包装成一个jQuery元素. 格式:angular.element(element); element:包装成jquery对象的html ...
- [Angular] Angular CDK Intro
1. Installl latest @angular/cli: sudo npm i -g @angular/cli@next The version I used is:6.0.0-rc.10 2 ...
随机推荐
- 18.链表管理内存实现c语言自动释放内存
运行截图: 创建记录分配的内存地址大小和地址的结构体 struct MEM { void *p; int size; }; 创建管理内存结构体的链表 typedef struct LinkNode { ...
- javafx DragDropped file
public class EffectTest extends Application { @Override public void start(Stage primaryStage) { Grou ...
- HDU 1533 Going Home(KM完美匹配)
HDU 1533 Going Home 题目链接 题意:就是一个H要相应一个m,使得总曼哈顿距离最小 思路:KM完美匹配,因为是要最小.所以边权建负数来处理就可以 代码: #include <c ...
- apache2 虚拟机多用户多站点设置 mpm-itk
MPM设置 https://bbs.csdn.net/topics/390479795/ vim /opt/lampp/etc/extra/httpd-ssl.conf vim /opt/lampp/ ...
- (转)Linux下使用rsync最快速删除海量文件的方法
转自 : http://www.ha97.com/4107.html 昨天遇到了要在Linux下删除海量文件的情况,需要删除数十万个文件.这个是之前的程序写的日志,增长很快,而且没什么用.这个时候,我 ...
- 【Codeforces Round #429 (Div. 2) A】Generous Kefa
[Link]:http://codeforces.com/contest/841/problem/A [Description] [Solution] 模拟,贪心,每个朋友尽量地多给气球. [Numb ...
- 《WPF》Expander控件简单美化
示例图: Expander控件功能很常见, 一般用于系统左侧的菜单收缩面板. 1.主要的组成 一个头部(header) 和 一个 内容(content) 组成. <Expander Expand ...
- virtualtemplate 接口
虚拟接口的配置.建立.与实际接口的关联 VPN在会话连接建立之后.须要创建一个虚拟接口用于和对端之间数据传输.此时,将依照用户配置,选择一个虚拟接口模板,动态地创建一个虚拟接口. 该接口将在会话结束时 ...
- HDU 4869 Turn the pokers(思维+组合公式+高速幂)
pid=4869" target="_blank">Turn the pokers 大意:给出n次操作,给出m个扑克.然后给出n个操作的个数a[i],每一个a[i] ...
- 如何将String类型转换成任意基本类型
[原创][C#] 如何将String类型转换成任意基本类型 Posted on 2009-12-02 09:47 YCOE 阅读( 2843) 评论( 14) 编辑 收藏 前几天,在写一个自动 ...