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的更多相关文章

  1. [Angular 6] 初学angular,环境全部最新,[ ng serve ] 不能启动,卡在 95% 不动 => 解决方案

    2018.9.7 问题描述: 通过ng serve命令启动angular应用时,卡在95%, ctrl+c 停掉后看到错误内容为找不到ng_modules下的angular模块下的package.js ...

  2. [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, ...

  3. Angular template ng-template/container/content

    1. ng-template 形式:<ng-template>...</ng-template> 默认ng-template中的内容会隐藏; 可通过[ngIf]来控制内容显示隐 ...

  4. [Angular] Change component default template (ng-content, ng-template, ngTemplateOutlet, TemplateRef)

    Here is the defulat tab header template: <ng-template #defaultTabHeader let-tabs="tabsX" ...

  5. [Angular Directive] Structure directive and <template>

    The structure directive is just a sugar syntax of <template>. Such as: <div *ngIf="nam ...

  6. [Angular] Angular Elements Intro

    Make sure install the latest Angular v6 with Angular CLI. Checkout ght Github for the code. 1. Creat ...

  7. Angular - - angular.bind、angular.bootstrap、angular.copy

    angular.bind 返回一个调用self的函数fn(self代表fn里的this).可以给fn提供参数args(*).这个功能也被称为局部操作,以区别功能. 格式:angular.bind(se ...

  8. Angular - - angular.element

    angular.element 将DOM元素或者HTML字符串一包装成一个jQuery元素. 格式:angular.element(element); element:包装成jquery对象的html ...

  9. [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 ...

随机推荐

  1. 6.Maven之(六)setting.xml配置文件详解

    转自:https://blog.csdn.net/u012152619/article/details/51485152

  2. 关于router-link的传参以及参数的传递

    1.路径:http://localhost:8081/#/test?name=1 <router-link :to="{path:'/test',query: {name: id}}& ...

  3. TensorFlow的学习

    1.先判断python的版本(因为有些python版本自带pip,可以参考我写的对pip的认识的博客文章),选择是否安装pip,然后安装更新tensorflow如:sudo pip install - ...

  4. MATLAB 软件学习

    what  列出当前目录或指定目录下的M\MAT 和 MAX 文件 …   在语句行尾端表示该行未完 !  调用操作系统的命令 isvarname  判断变量名是否有效 声明全局变量   变量名前加 ...

  5. Unity 3D 开发 —— 脚本编程

    Unity 相关资源 Unity 官网 :http://www.unity3D.com Unity 论坛 : http://forum.unity3d.com/forum.php Unity 问答 : ...

  6. [RxJS] Marbles Testings

    Install: npm install — save-dev jasmine-marbles Basic example: import {cold, getTestScheduler} from ...

  7. [Android] 图像各种处理系列文章合集

        这是我近期在做Android随手拍一个项目的各种网上关于图片处理的资料,曾经学过数字图像处理都是用C++写的,以下的资料个人觉得是很优秀的各种集合,还有一方面它是在线笔记,希望对大家有所帮助吧 ...

  8. RingtoneManager-获得系统当前的铃声

    我们直接看代码 bt1 = (Button) findViewById(R.id.bt1); bt2 = (Button) findViewById(R.id.bt2); bt3 = (Button) ...

  9. php中类的持久化如何实现

    php中类的持久化如何实现 一.总结 一句话总结:PHP持久化通过serialize()  和   unserialize() 这两个函数来实现的. 1.持久化之后的对象保存到哪里? 将复杂的数组之类 ...

  10. Python 标准库 csv —— csv 文件的读写

    csv 文件,逗号分割文件. 0. 读取 csv 到 list from csv import reader def load_csv(csvfile): dataset = [] with open ...