Angular 2 ViewChild & ViewChildren
一、ViewChild
ViewChild 是属性装饰器,用来从模板视图中获取匹配的元素。视图查询在 ngAfterViewInit 钩子函数调用前完成,因此在 ngAfterViewInit 钩子函数中,才能正确获取查询的元素。
1.@ViewChild 使用模板变量名
import { Component, ElementRef, ViewChild, AfterViewInit } from '@angular/core'; @Component({
selector: 'my-app',
template: `
<h1>Welcome to Angular World</h1>
<p #greet>Hello {{ name }}</p>
`,
})
export class AppComponent {
name: string = 'Semlinker'; @ViewChild('greet')
greetDiv: ElementRef; ngAfterViewInit() {
console.dir(this.greetDiv);
}
}
2.@ViewChild 使用模板变量名及设置查询条件
import { Component, TemplateRef, ViewChild, ViewContainerRef, AfterViewInit } from '@angular/core'; @Component({
selector: 'my-app',
template: `
<h1>Welcome to Angular World</h1>
<template #tpl>
<span>I am span in template</span>
</template>
`,
})
export class AppComponent { @ViewChild('tpl')
tplRef: TemplateRef<any>; @ViewChild('tpl', { read: ViewContainerRef })
tplVcRef: ViewContainerRef; ngAfterViewInit() {
console.dir(this.tplVcRef);
this.tplVcRef.createEmbeddedView(this.tplRef);
}
}
3.@ViewChild 使用类型查询
//child.component.ts
import { Component, OnInit } from '@angular/core'; @Component({
selector: 'exe-child',
template: `
<p>Child Component</p>
`
})
export class ChildComponent {
name: string = 'child-component';
}
//app.component.ts
import { Component, ViewChild, AfterViewInit } from '@angular/core';
import { ChildComponent } from './child.component'; @Component({
selector: 'my-app',
template: `
<h4>Welcome to Angular World</h4>
<exe-child></exe-child>
`,
})
export class AppComponent { @ViewChild(ChildComponent)
childCmp: ChildComponent; ngAfterViewInit() {
console.dir(this.childCmp);
}
}
以上代码运行后,控制台的输出结果:
二、ViewChildren
ViewChildren 用来从模板视图中获取匹配的多个元素,返回的结果是一个 QueryList 集合。
@ViewChildren 使用类型查询
import { Component, ViewChildren, QueryList, AfterViewInit } from '@angular/core';
import { ChildComponent } from './child.component'; @Component({
selector: 'my-app',
template: `
<h4>Welcome to Angular World</h4>
<exe-child></exe-child>
<exe-child></exe-child>
`,
})
export class AppComponent { @ViewChildren(ChildComponent)
childCmps: QueryList<ChildComponent>; ngAfterViewInit() {
console.dir(this.childCmps);
}
}
以上代码运行后,控制台的输出结果:
文章转自:https://segmentfault.com/a/1190000008695459
Angular 2 ViewChild & ViewChildren的更多相关文章
- Angular ViewChild & ViewChildren
基础 ViewChild ViewChild 装饰器用于获取模板视图中的元素或直接调用其组件中的方法.它支持 Type 类型或 string 类型的选择器,同时支持设置 read 查询条件,以获取不同 ...
- [Angular 2] @ViewChild to access Child component's method
When you want to access child component's method, you can use @ViewChild in the parent: Parent Compo ...
- Angular利用@ViewChild在父组件执行子组件的方法
代码如下: @Component({ selector: 'my-app', template: ` <step-bar #stepBar></step-bar> ` }) e ...
- Angular中ViewChild\ngAfterViewInit\Promise的使用,在父组件初始化时等待子组件的返回值
1.子component中的异步方法 initCreateJob = () => new Promise((resolve, reject) => { setTimeout(() => ...
- Angular4学习笔记(七)- ViewChild和ViewChildren
基础 ViewChild ViewChild 装饰器用于获取模板视图中的元素或直接调用其组件中的方法.它支持 Type 类型或 string 类型的选择器,同时支持设置 read 查询条件,以获取不同 ...
- Angular ViewChild
viewchild // 使用方法 git clone https://git.oschina.net/mumu-osc/learn-component.git cd learn-component ...
- Angular5 父组件获取子组件实例( ViewChildren、ViewChild用法)
原文链接 Understanding ViewChildren, ContentChildren, and QueryList in Angular 使用场景 有时候,我们想要在父组件中访问它的子组件 ...
- Angular Viewchild undefined
Angular的viewchild在使用的时候报错 undefined 1 检查是否在元素上打上标识 #xxx 2 查看引用元素时的时机 是否在AfterViewInit之后 3 检查元素是否在*ng ...
- Angular @ViewChild,Angular 中的 dom 操作
Angular 中的 dom 操作(原生 js) ngAfterViewInit(){ var boxDom:any=document.getElementById('box'); boxDom.st ...
随机推荐
- 【Python基础】之for循环、数组字典
一. for循环实例 1.循环字符串 Python Shell: for i in "hello": print(i) h e l l o 2.循环数组Python Shell: ...
- Top 10 Open Source Bug Tracking System系统
Bugzilla http://www.bugzilla.org/ Mantis php http://www.mantisbt.org/ Trac Python also provides wiki ...
- 小程序框架MpVue踩坑日记(一)
小程序也做了几个小功能模块了,总觉得需要总结一下,踩坑什么的还是得记录一下啊. 好吧,其实是为了方便回顾 首先,说到小程序框架,大家都知道wepy,不过,我是没用过 美团开发团队到mpvue到是个实在 ...
- PowerBuilder -- 条码打印
# 使用ocx控件 使用微软的MSBCODE9.OCX,但是注册老不成功,需要安装office之后才能注册成功,不知道有没有好的处理方法?? # 使用字体 字体下载:http://download.c ...
- hdu2473 Junk-Mail Filter 并查集+删除节点+路径压缩
Description Recognizing junk mails is a tough task. The method used here consists of two steps: 1) ...
- Project Euler:Problem 41 Pandigital prime
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly o ...
- iPhone,iPad如何获取WIFI名称即SSID
本文转载至 http://blog.csdn.net/wbw1985/article/details/20530281 2010年开始苹果清理了一批APP Store上的WIFI扫描软件, 缘由语焉 ...
- Win10上Python3通过pip安装时出现UnicodeDecodeError
http://blog.csdn.net/qq_33530388/article/details/68933201 解决方法: 打开 c:\program files\python36\lib\sit ...
- 【BZOJ3834】[Poi2014]Solar Panels 分块好题
[BZOJ3834][Poi2014]Solar Panels Description Having decided to invest in renewable energy, Byteasar s ...
- 【BZOJ1222】[HNOI2001]产品加工 DP
[BZOJ1222][HNOI2001]产品加工 Description 某加工厂有A.B两台机器,来加工的产品可以由其中任何一台机器完成,或者两台机器共同完成.由于受到机器性能和产品特性的限制,不同 ...