要解决该问题,我们可以使用 <ng-template> 的标准语法 (非*ngIf 语法糖): <ng-template [ngIf]="show">
In template, no attributes. 即 <ng-template> 中的内容不会显示。 最后我们来总结一下 <ng-template> 和 <ng-container> 的区别: <ng-template> :使用 * 语法糖的结构指令,最终都会转换为 <ng-template> 或 <template
组件模板: <ng-template #tpl>
注意,else 绑定指向的是一个带有 #elseBlock 标签的 <ng-template> 元素。 该模板可以定义在此组件视图中的任何地方,但为了提高可读性,通常会放在 ngIf 的紧下方。 text while primary text is hidden</ng-template> then
接下来模板元素 <ng-template> 将会作为我们的组件容器,具体示例如下: import { Component } from '@angular/core'; @Component({ selector: 'my-app', template: ` <ng-template #alertContainer></ng-template> ` }) export class #alertContainer></ng-template> ` }) export class AppComponent { @ViewChild("alertContainer", { read #alertContainer></ng-template> <button (click)="createComponent('success')">Create success alert @Component({ selector: 'my-app', template: ` <ng-template #alertContainer></ng-template>
有条件的内容投影 中文网的描述: 如果你的组件需要_有条件地_渲染内容或多次渲染内容,则应配置该组件以接受一个 ng-template 元素,其中包含要有条件渲染的内容。 使用 ng-template 元素,你可以让组件根据你想要的任何条件显式渲染内容,并可以进行多次渲染。在显式渲染 ng-template 元素之前,Angular 不会初始化该元素的内容。 使用ng-container定义我们的投影区块 使用ngTemplateOutlet指令来渲染ng-template元素。 通过内置的动态指令*ngIf来控制是否渲染投影。 <app-page-container>
</ng-template> `, }) export class AppComponent implements AfterViewInit{ @ViewChild('tpl') tplRef </ng-template> <ng-template #atpl> Hello, Angular! #stpl let-message="message">
{{message}}
</ng-template> <ng-template #atpl let-msg ="message">{{msg}}
</ng-template> <ng-template #otpl let-msg>{{msg}}< 微语法解析器把这个字符串转换成 <ng-template> 上的属性。
06 Use <ng-template> 原文: Use <ng-template> Render Props最近在React社区中引起了轰动,但是与之类似的模式在Angular中似乎并没有得到太多关注 Note: TemplateRef是一个类名而<ng-template>是一个html标签,它们本质上是相同的。 不过你可能会在项目中更频繁地使用<ng-template>,但是在网上你可以很容易的搜索到关于TemplateRef的知识,因为<ng-template>会给你提供很多html5中的<template> 实现 <ng-template>组件可以完美地解决问题。 1. 父组件 从toggle组件中传入的状态是通过let关键字在父组件的<ng-template>标签上显示声明的。
06 Use <ng-template> 原文: Use <ng-template> Render Props最近在React社区中引起了轰动,但是与之类似的模式在Angular中似乎并没有得到太多关注 Note: TemplateRef是一个类名而<ng-template>是一个html标签,它们本质上是相同的。 不过你可能会在项目中更频繁地使用<ng-template>,但是在网上你可以很容易的搜索到关于TemplateRef的知识,因为<ng-template>会给你提供很多html5中的<template> 实现 <ng-template>组件可以完美地解决问题。 1. 父组件 从toggle组件中传入的状态是通过let关键字在父组件的<ng-template>标签上显示声明的。
示例 前面我们已经介绍了如何使用 HTML5 template 模板元素,下面我们来看一下如何使用 <ng-template> 元素。 @Component({ selector: "hello-world", template: `
image.png 也可以通过 <nz-back-top [nzTemplate]="tpl" [nzVisibilityHeight]="100" (nzOnClick)="notify()"> <ng-template #tpl>
---- 问题1:template标签已经废弃了 信息来源:启动的时候控制台有提示,官方日志也有说 解决方案:全局搜索<template></template> ,替换<ng-template></ng-template ** ---- NG4的亮点 新的视图引擎,据说能让渲染更快 加强了*ngIf,里面可以写else了,这里直接拿官方的写法<ng-template #loading>Loading... </ng-template>
普通方法 <mtx-grid [data]="list" [columns]="columns"> </mtx-grid> <ng-template #statusTpl let-row </mat-slide-toggle> </ng-template> export class AppComponent implements OnInit { @ViewChild('statusTpl data]="list" [columns]="columns" [cellTemplate]="{ city: cityTpl }"> </mtx-grid> <ng-template "columns" [expandable]="true" [expansionTemplate]="expansionTpl"> </mtx-grid> <ng-template #expansionTpl let-row> {{row.name}} </ng-template> 在列定义中设置 showExpand, 确定在哪个列显示展开符号。
<any *ngIf=“布尔表达式”;else ELSE块的编号> <ng-template # ELSE块的编号> … 举例: html文件 <div *ngIf="isPayingUser </div> <<em>ng-template</em> #forChildren>此处是未成年的宝宝看的...</<em>ng-template</em>> <! -- ngIf,else此处只能用NG模板元素<em>ng-template</em>,该容器可以存放其他标签 --> ts文件: isPayingUser = true;//该用户是否为付费用户 //isPayingUser
meta charset="utf-8"> <title></title> </head> <body>
组件和相关的 Connection 指令: connection.component.ts @Component({ selector: 'connection', template: ` <ng-template [ngTemplateOutlet]="fast.tpl" *ngIf="isFast"></ng-template> <ng-template [ngTemplateOutlet]="slow.tpl isFast"></ng-template> `, }) export class ConnectionComponent { isFast = true; @ContentChild(FastDirective
一、如何组件自定义输入内容 比如在一个页面上,引用nz-card时,把页面上<ng-template>内容插入到nz-card中去, ? ngTemplateOutlet 完整示意-----我是主页 <app-content [dynamicRef]="usedByContent"></app-content> <ng-template
The solution 为了让包装器能够控制其子元素的实例化,我们可以通过两种方式完成:在我们的内容周围使用 <ng-template> 元素,或者使用带有 “*” 语法的结构指令。 为简单起见,我们将在示例中使用 <ng-template> 语法,我们的新应用程序如下所示: <wrapper> <ng-template> <counter></counter> </ ng-template> </wrapper> 包装器不再使用 <ng-content>,因为它接收到一个模板。
li>test
<script type="text/<em>ng-template</em> </script> <script type="text/ng-template" id="'page3.html'"> this is page 1 for test. </script> <script type="text/<em>ng-template</em>" id="'page2.html'"> this is page 2 for app. </script> <script type="text/<em>ng-template</em>" id="'page3.html'"> this is page 1 for test. </script> <script type="text/<em>ng-template</em>" id="'page4.html'"> this is page 2 for test.解决方法 为了让组件能够控制投射进来的子组件的实例化,我们可以通过两种方式完成:在我们的内容周围使用 <ng-template> 元素及 ngTemplateOutlet,或者使用带有 "*" 语法的结构指令 为简单起见,我们将在示例中使用 <ng-template> 语法。 show = true; constructor() { } ngOnInit() { } } 然后我们将 demo-child-component 包含在 ng-template 中: <demo-component> <ng-template> <demo-child-component></demo-child-component> </ng-template