[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 ...
随机推荐
- solr简介
Solr 是什么? Solr它是一种开放源码的.基于 Lucene Java 的搜索服务器,易于加入到 Web 应用程序中. Solr 提供了层面搜索(就是统计).命中醒目显示并且支持多种输出格式(包 ...
- jq实现回车键执行方法
$(function(){ $(document).keypress(function (e){ if(e.keyCode == 13){ //执行你想执行的方法,keyCode代表不同的按键 } } ...
- Windows学习总结(3)——成为电脑高手必备的cmd命令大全
曾经看电影和电视里面电脑黑客快速敲击电脑键盘,一行行命令在电脑屏幕闪过,一个回车过后,一排排英文象走马灯一样在屏幕上转瞬即逝,那才是我们梦寐以求的高手,有木有!实际上,不光是黑客和系统维护人员,一般的 ...
- 查看oracle数据库的启动时间
Oracle的sys用户下有个视图v_$instance,该视图只有一行数据.通过SQL语名可查询其内容: select * from sys.v_$instance 此视图可查看很多东西,如实例名, ...
- POJ 1442 Black Box treap求区间第k大
题目来源:POJ 1442 Black Box 题意:输入xi 输出前xi个数的第i大的数 思路:试了下自己的treap模版 #include <cstdio> #include < ...
- ubuntu终端sudo java提示“command not found”解决办法
我在ubuntu 12.04里想启动一个java程序,sudo java -jar xxx.jar,但是结果提示sudo:java:command not found. Ubuntu下用sudo运行j ...
- onvif开发实战1--总结框架搭建
Gsoap及开发框架生成: 一:gsoap下载和编译 1.下载Gsoap:地址:http://sourceforge.net/projects/gsoap2/files/gSOAP/ 2.安装: ...
- vue-cli 和webpack
https://note.youdao.com/share/?id=d1851db9f0fe0a691798fac823849564&type=notebook#/C045BC3E7DC144 ...
- 在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) ...
- 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 ...