Previously we have tab-panel template defined like this:

  1. <ul class="tab-panel-buttons" *ngIf="tabs">
  2. <li
  3. [ngClass]="{selected: tab.selected}"
  4. (click)="selectTab(tab)"
  5. *ngFor="let tab of tabs;">{{tab.title}}
  6. </li>
  7. </ul>
  8.  
  9. <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.

  1. <ng-template #defaultTabHeader>
  2. <ul class="tab-panel-buttons" *ngIf="tabs">
  3. <li
  4. [ngClass]="{selected: tab.selected}"
  5. (click)="selectTab(tab)"
  6. *ngFor="let tab of tabs;">{{tab.title}}
  7. </li>
  8. </ul>
  9. </ng-template>

To be able to render the template to the DOM; we need to use <ng-content>:

  1. <ng-template #defaultTabHeader let-tabs="tabsX">
  2. <ul class="tab-panel-buttons" *ngIf="tabs">
  3. <li
  4. [ngClass]="{selected: tab.selected}"
  5. (click)="selectTab(tab)"
  6. *ngFor="let tab of tabs;">{{tab.title}}
  7. </li>
  8. </ul>
  9. </ng-template>
  10.  
  11. <ng-content *ngTemplateOutlet="defaultTabHeader; context: tabsContext"></ng-content>
  12.  
  13. <ng-content></ng-content>
  1. import {AfterContentInit, Component, ContentChildren, OnInit, QueryList} from '@angular/core';
  2. import {AuTabComponent} from '../au-tab/au-tab.component';
  3.  
  4. @Component({
  5. selector: 'au-tab-panel',
  6. templateUrl: './au-tab-panel.component.html',
  7. styleUrls: ['../tab-panel.component.scss']
  8. })
  9. export class AuTabPanelComponent implements OnInit, AfterContentInit {
  10.  
  11. @ContentChildren(AuTabComponent)
  12. tabs: QueryList<AuTabComponent>;
  13.  
  14. constructor() { }
  15.  
  16. ngOnInit() {
  17. }
  18.  
  19. ngAfterContentInit(): void {
  20. const selectedTab = this.tabs.find(tab => tab.selected);
  21. if(!selectedTab && this.tabs.first) {
  22. this.tabs.first.selected = true;
  23. }
  24. }
  25.  
  26. selectTab(tab: AuTabComponent) {
  27. this.tabs.forEach(t => t.selected = false);
  28. tab.selected = true;
  29. }
  30.  
  31. get tabsContext() {
  32. return {
  33. tabsX: this.tabs
  34. };
  35. }
  36.  
  37. }

[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. solr简介

    Solr 是什么? Solr它是一种开放源码的.基于 Lucene Java 的搜索服务器,易于加入到 Web 应用程序中. Solr 提供了层面搜索(就是统计).命中醒目显示并且支持多种输出格式(包 ...

  2. jq实现回车键执行方法

    $(function(){ $(document).keypress(function (e){ if(e.keyCode == 13){ //执行你想执行的方法,keyCode代表不同的按键 } } ...

  3. Windows学习总结(3)——成为电脑高手必备的cmd命令大全

    曾经看电影和电视里面电脑黑客快速敲击电脑键盘,一行行命令在电脑屏幕闪过,一个回车过后,一排排英文象走马灯一样在屏幕上转瞬即逝,那才是我们梦寐以求的高手,有木有!实际上,不光是黑客和系统维护人员,一般的 ...

  4. 查看oracle数据库的启动时间

    Oracle的sys用户下有个视图v_$instance,该视图只有一行数据.通过SQL语名可查询其内容: select * from sys.v_$instance 此视图可查看很多东西,如实例名, ...

  5. POJ 1442 Black Box treap求区间第k大

    题目来源:POJ 1442 Black Box 题意:输入xi 输出前xi个数的第i大的数 思路:试了下自己的treap模版 #include <cstdio> #include < ...

  6. ubuntu终端sudo java提示“command not found”解决办法

    我在ubuntu 12.04里想启动一个java程序,sudo java -jar xxx.jar,但是结果提示sudo:java:command not found. Ubuntu下用sudo运行j ...

  7. onvif开发实战1--总结框架搭建

    Gsoap及开发框架生成: 一:gsoap下载和编译   1.下载Gsoap:地址:http://sourceforge.net/projects/gsoap2/files/gSOAP/ 2.安装: ...

  8. vue-cli 和webpack

    https://note.youdao.com/share/?id=d1851db9f0fe0a691798fac823849564&type=notebook#/C045BC3E7DC144 ...

  9. 在Sql中将 varchar 值 '1,2,3,4,5,6' 转换成数据类型 int

    --问题:将aa转换为Int类型失败 string aa="3,5,11,56,88,45,23"; select * from ERPBuMen where ID in(aa) ...

  10. position(static-relative-absolute-fixed),margin(top-right-bottom-left),top-right-bottom-left

    最近写css遇到一些问题,所以准备写下来捋一下思路. 1.position=satic下看margin的使用.(top-right-bottom-left在这种case下无效) 1-1)margin ...