[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 ...
随机推荐
- 56.lambda表达式与绑定以及伪函数和绑定
#include <iostream> #include <functional> using namespace std; using namespace std::plac ...
- Linux获取进程中变量
列出所有进程 #include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h> ...
- idea配置spark运行模式
1. 配置运行参数: Menu -> Run -> Edit Configurations -> 选择 + -> Application -Dspark.master=lo ...
- deep-in-es6(二)
es6-生成器Generators: eg: function* quips(name) { yield "您好"+name+"!"; if(name.star ...
- 2019.05.08 《Linux驱动开发入门与实战》
第六章:字符设备 申请设备号---注册设备 1.字符设备的框架: 2.结构体,struct cdev: 3.字符设备的组成: 4.例子: 5.申请和释放设备号: 设备号和设备节点是什么关系.? 设备驱 ...
- Git学习总结(2)——初识 GitHub
1. 写在前面 我一直认为 GitHub 是程序员必备技能,程序员应该没有不知道 GitHub 的才对,没想到这两天留言里给我留言最多的就是想让我写关于 GitHub 的教程,说看了不少资料还是一头雾 ...
- wmic linux python
sudo aptitude install wmi-client Example of usage is; wmic -U DOMAIN/administrator%password //10.99. ...
- ORA-01031: 权限不足
1.错误描写叙述 ORA-01031: 权限不足 2.错误原因 SQL> create user yhd identified by scott account unlock; create u ...
- RK3066 实现LED闪烁的代码分析
实现LED灯的闪烁,须要在驱动里加入一个定时器函数,详细实现涉及到了LED GPIO驱动.用户空间程序调用驱动程序. 1.首先来看LED设备驱动注冊过程,代码位于../kernel/drivers/l ...
- [REASONML] Using Javascript npm package from REASON
For example, we want to use moment.js inside our ReasonML code. What we can do is create a module fi ...