1. <ng-template>元素
import { Component, TemplateRef, ViewContainerRef, ViewChild,
AfterViewInit } from '@angular/core';
@Component({
selector: 'app-code404',
template: `
<!-- 这里使用一个模板变量,在组件中使用@ViewChild装饰器获取模板元素-->
<ng-template #tpl>
Big Keriy !
</ng-template>
`,
})
export class Code404Component implements AfterViewInit{
// @ViewChild 装饰器获取模板元素
@ViewChild('tpl')
tplRef: TemplateRef<any>;
constructor(private vcRef: ViewContainerRef) {}
ngAfterViewInit() {
// 使用ViewContainerRef对象的createEmbeddedView方法创建内嵌视图。
this.vcRef.createEmbeddedView(this.tplRef);
} }
这样其实我们在视图中就得到了一个什么...啊,就是一个'Big Keriy !'的字符串。
2. ngTemplateOutlet指令
a. ngTemplateOutlet
和routerOutlet是一个意思,将视图(<ng-template>标签中的内容)放到对应的ngTemplateoutlet下面。
import { Component } from '@angular/core';
@Component({
selector: 'app-code404',
template: `
<ng-template #stpl>
Hello, Semlinker!
</ng-template>
<ng-template #atpl>
Big Keriy !
</ng-template>
<div [ngTemplateOutlet]="atpl"></div>
<div [ngTemplateOutlet]="stpl"></div>
`, })
export class Code404Component { }
最终的视图应该是:
Big Keriy !
Hello, Semlinker!
b. ngOutletContex
看名字就知道意思。
ngTemplateOutlet指令基于TemplateRef对象,在使用ngTemplateOutlet指令时,可以通过ngTemplateOutletContext属性来设置来设置EmbeddedViewRef的上下文对象。可以使用let语法来声明绑定上下文对象属性名。
import { Component, TemplateRef, ViewContainerRef, ViewChild,
AfterViewInit } from '@angular/core';
@Component({
selector: 'app-code404',
template: `
<!-- 这里的messagey映射到下面context中message 再使用插值表达式的方式显示message的值 -->
<ng-template #stpl let-message="message">
<p>{{message}}</p>
</ng-template>
<!-- 这里的messagey映射到下面context中message , let-msg是一种与语法糖的方式变量名是msg-->
<ng-template #atpl let-msg="message">
<p>{{msg}}</p>
</ng-template>
<!-- 若不指定变量值那么将显示 $implicit 的值-->
<ng-template #otpl let-msg>
<p>{{msg}}</p>
</ng-template>
<div [ngTemplateOutlet]="atpl"
// 这里ngOutletContext绑定的是context对象
[ngOutletContext]="context">
</div>
<div [ngTemplateOutlet]="stpl"
[ngOutletContext]="context">
</div>
<div [ngTemplateOutlet]="otpl"
[ngOutletContext]="context">
</div>
`,
})
export class Code404Component implements AfterViewInit{
@ViewChild('tpl')
tplRef: TemplateRef<any>;
constructor(private vcRef: ViewContainerRef) {}
ngAfterViewInit() {
this.vcRef.createEmbeddedView(this.tplRef);
}
context = { message: 'Hello ngOutletContext!',
$implicit: 'great, Semlinker!' };
// 这里的$implicit是固定写法
}
先看输出的视图:
Hello ngOutletContext!
Hello ngOutletContext!
Hello, Semlinker!
3. ngComponentOutlet指令
听着名字就很爽,这不是插入视图的,是插入组件的!
该指令使用声明的方式,动态加载组件。
先写组件,里面有两个。。组件:
@Component({
selector: 'alert-success',
template: `
<p>Alert success</p>
`,
})
export class AlertSuccessComponent { }
@Component({
selector: 'alert-danger',
template: `
<p>Alert danger</p>
`,
})
export class AlertDangerComponent { }
@Component({
selector: 'my-app',
template: `
<h1>Angular version 4</h1>
<ng-container *ngComponentOutlet="alert"></ng-container>
<button (click)="changeComponent()">Change component</button>
`, })
export class AppComponent {
alert = AlertSuccessComponent;
changeComponent() {
this.alert = AlertDangerComponent;
}
}
当然,还需要在模块中声明入口:
// app.module.ts
@NgModule({
// ...
declarations: [
AppComponent,
SignUpComponent,
AlertSuccessComponent,
AlertDangerComponent
],
entryComponents: [ // 这里面写指令中呀用到的组件
AlertSuccessComponent,
AlertDangerComponent
],
// ...
})
这样就可以使用ngComponentOutlet指令来插入组件玩耍了:
<!-- 简单语法 --> <ng-container *ngComponentOutlet="componentTypeExpression"></ng-container> <!-- 完整语法 --> <ng-container *ngComponentOutlet="componentTypeExpression; injector: injectorExpression; content: contentNodesExpression;"> </ng-container>
这是一个完整语法简单的例子:
// ...
@Component({
selector: 'ng-component-outlet-complete-example',
template: `
<ng-container *ngComponentOutlet="CompleteComponent;
injector: myInjector;
content: myContent"></ng-container>`
})
class NgTemplateOutletCompleteExample {
// This field is necessary to expose CompleteComponent to the template.
CompleteComponent = CompleteComponent;
myInjector: Injector;
myContent = [[document.createTextNode('Ahoj')], [document.createTextNode('Svet')]];
constructor(injector: Injector) {
this.myInjector = ReflectiveInjector.resolveAndCreate([Greeter], injector);
}
}
4. 创建结构指令
也想不出来一个什么好例子,抄一个例子过来:
// uless.directive.ts
import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core';
@Directive({
selector: '[exeUnless]'
})
export class UnlessDirective {
@Input('exeUnless')
set condition(newCondition: boolean) { // set condition
if (!newCondition) {
this.viewContainer.createEmbeddedView(this.templateRef);
} else {
this.viewContainer.clear();
}
}
constructor(private templateRef: TemplateRef<any>,
private viewContainer: ViewContainerRef) {
}
}
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `
<h2 *exeUnless="condition">Hello, Semlinker!</h2>
`,
})
export class AppComponent {
condition: boolean = false;
}
// app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `
<h2 *exeUnless="condition">Hello, Semlinker!</h2>
`,
})
export class AppComponent {
condition: boolean = false;
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# Angular
# 自定义结构指令
# 结构指令
# Angular 2.x学习教程之结构指令详解
# 详解angular2 控制视图的封装模式
# 浅谈Angular 观察者模式理解
# angular 服务的单例模式(依赖注入模式下)详解
# Angular指令之restict匹配模式的详解
# Angular中的结构指令模式及使用详解
# 绑定
# 的是
# 是一个
# 是一种
# 要在
# 这是一个
# 可以通过
# 这不是
# 可以使用
# 也想
# 这里面
# 若不
# 中就
# 还需
# 先看
# 大家多多
# 就可以
# 内嵌
# 很爽
# 应该是
相关文章:
建站之星五站合一营销型网站搭建攻略,流量入口全覆盖优化指南
,巨量百应是干嘛的?
小程序网站制作需要准备什么资料,如何制作小程序?
如何选择美橙互联多站合一建站方案?
如何快速搭建FTP站点实现文件共享?
测试制作网站有哪些,测试性取向的权威测试或者网站?
网站微信制作软件,如何制作微信链接?
如何在Mac上搭建Golang开发环境_使用Homebrew安装和管理Go版本
建站与域名管理如何高效结合?
专业型网站制作公司有哪些,我设计专业的,谁给推荐几个设计师兼职类的网站?
已有域名和空间如何搭建网站?
邀请函制作网站有哪些,有没有做年会邀请函的网站啊?在线制作,模板很多的那种?
在线教育网站制作平台,山西立德教育官网?
如何正确选择百度移动适配建站域名?
郑州企业网站制作公司,郑州招聘网站有哪些?
建站之星手机一键生成:多端自适应+小程序开发快速建站指南
如何快速搭建高效简练网站?
高防服务器租用如何选择配置与防御等级?
Thinkphp 中 distinct 的用法解析
网站制作壁纸教程视频,电脑壁纸网站?
建站主机默认首页配置指南:核心功能与访问路径优化
如何安全更换建站之星模板并保留数据?
如何在Golang中使用replace替换模块_指定本地或远程路径
c# Task.ConfigureAwait(true) 在什么场景下是必须的
淘宝制作网站有哪些,淘宝网官网主页?
金*站制作公司有哪些,金华教育集团官网?
如何在阿里云香港服务器快速搭建网站?
在线制作视频网站免费,都有哪些好的动漫网站?
免费的流程图制作网站有哪些,2025年教师初级职称申报网上流程?
已有域名能否直接搭建网站?
如何在沈阳梯子盘古建站优化SEO排名与功能模块?
建站之星客服服务时间及联系方式如何?
如何基于云服务器快速搭建网站及云盘系统?
网站规划与制作是什么,电子商务网站系统规划的内容及步骤是什么?
三星网站视频制作教程下载,三星w23网页如何全屏?
建站之星后台管理系统如何操作?
建站之星后台管理:高效配置与模板优化提升用户体验
教学论文网站制作软件有哪些,写论文用什么软件
?
香港服务器网站搭建教程-电商部署、配置优化与安全稳定指南
高性价比服务器租赁——企业级配置与24小时运维服务
如何通过免费商城建站系统源码自定义网站主题与功能?
javascript中对象的定义、使用以及对象和原型链操作小结
c# 在ASP.NET Core中管理和取消后台任务
如何在香港服务器上快速搭建免备案网站?
手机网站制作与建设方案,手机网站如何建设?
建站之星2.7模板快速切换与批量管理功能操作指南
唐山网站制作公司有哪些,唐山找工作哪个网站最靠谱?
建站主机选购指南:核心配置优化与品牌推荐方案
php能控制zigbee模块吗_php通过串口与cc2530 zigbee通信【介绍】
如何用wdcp快速搭建高效网站?
*请认真填写需求信息,我们会在24小时内与您取得联系。