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中的结构指令模式及使用详解
# 绑定
# 的是
# 是一个
# 是一种
# 要在
# 这是一个
# 可以通过
# 这不是
# 可以使用
# 也想
# 这里面
# 若不
# 中就
# 还需
# 先看
# 大家多多
# 就可以
# 内嵌
# 很爽
# 应该是
相关文章:
免费ppt制作网站,有没有值得推荐的免费PPT网站?
网页设计网站制作软件,microsoft office哪个可以创建网页?
如何使用Golang安装API文档生成工具_快速生成接口文档
logo在线制作免费网站在线制作好吗,DW网页制作时,如何在网页标题前加上logo?
网站制作公司,橙子建站是合法的吗?
金*站制作公司有哪些,金华教育集团官网?
如何设计高效校园网站?
C#如何序列化对象为XML XmlSerializer用法
建站为何优先选择香港服务器?
如何破解联通资金短缺导致的基站建设难题?
北京网站制作费用多少,建立一个公司网站的费用.有哪些部分,分别要多少钱?
如何在宝塔面板中修改默认建站目录?
建站之星安装模板失败:服务器环境不兼容?
建站之星收费标准详解:套餐费用及年费价格表一览
建站之星后台密码遗忘或太弱?如何重置与强化?
如何通过可视化优化提升建站效果?
个人网站制作流程图片大全,个人网站如何注销?
如何在IIS7上新建站点并设置安全权限?
网站制作费用多少钱,一个网站的运营,需要哪些费用?
临沂网站制作企业,临沂第三中学官方网站?
如何通过虚拟主机快速搭建个人网站?
如何选择长沙网站建站模板?H5响应式与品牌定制哪个更优?
桂林网站制作公司有哪些,桂林马拉松怎么报名?
成都响应式网站开发,dw怎么把手机适应页面变成网页?
网站制作专业公司有哪些,如何制作一个企业网站,建设网站的基本步骤有哪些?
如何快速生成专业多端适配建站电话?
如何快速重置建站主机并恢复默认配置?
用v-html解决Vue.js渲染中html标签不被解析的问题
哪家制作企业网站好,开办像阿里巴巴那样的网络公司和网站要怎么做?
如何通过IIS搭建网站并配置访问权限?
如何自定义建站之星网站的导航菜单样式?
建站之星代理平台如何选择最佳方案?
网站制作网站,深圳做网站哪家比较好?
西安专业网站制作公司有哪些,陕西省建行官方网站?
如何登录建站主机?访问步骤全解析
如何在万网开始建站?分步指南解析
如何自己制作一个网站链接,如何制作一个企业网站,建设网站的基本步骤有哪些?
如何快速上传建站程序避免常见错误?
内部网站制作流程,如何建立公司内部网站?
淘宝制作网站有哪些,淘宝网官网主页?
江苏网站制作公司有哪些,江苏书法考级官方网站?
制作门户网站的参考文献在哪,小说网站怎么建立?
如何在阿里云虚拟服务器快速搭建网站?
娃派WAP自助建站:免费模板+移动优化,快速打造专业网站
开源网站制作软件,开源网站什么意思?
已有域名和空间如何快速搭建网站?
,柠檬视频怎样兑换vip?
网站制作员失业,怎样查看自己网站的注册者?
如何获取开源自助建站系统免费下载链接?
广东企业建站网站优化与SEO营销核心策略指南
*请认真填写需求信息,我们会在24小时内与您取得联系。