、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的更多相关文章

  1. Angular ViewChild & ViewChildren

    基础 ViewChild ViewChild 装饰器用于获取模板视图中的元素或直接调用其组件中的方法.它支持 Type 类型或 string 类型的选择器,同时支持设置 read 查询条件,以获取不同 ...

  2. [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 ...

  3. Angular利用@ViewChild在父组件执行子组件的方法

    代码如下: @Component({ selector: 'my-app', template: ` <step-bar #stepBar></step-bar> ` }) e ...

  4. Angular中ViewChild\ngAfterViewInit\Promise的使用,在父组件初始化时等待子组件的返回值

    1.子component中的异步方法 initCreateJob = () => new Promise((resolve, reject) => { setTimeout(() => ...

  5. Angular4学习笔记(七)- ViewChild和ViewChildren

    基础 ViewChild ViewChild 装饰器用于获取模板视图中的元素或直接调用其组件中的方法.它支持 Type 类型或 string 类型的选择器,同时支持设置 read 查询条件,以获取不同 ...

  6. Angular ViewChild

    viewchild // 使用方法 git clone https://git.oschina.net/mumu-osc/learn-component.git cd learn-component ...

  7. Angular5 父组件获取子组件实例( ViewChildren、ViewChild用法)

    原文链接 Understanding ViewChildren, ContentChildren, and QueryList in Angular 使用场景 有时候,我们想要在父组件中访问它的子组件 ...

  8. Angular Viewchild undefined

    Angular的viewchild在使用的时候报错 undefined 1 检查是否在元素上打上标识 #xxx 2 查看引用元素时的时机 是否在AfterViewInit之后 3 检查元素是否在*ng ...

  9. Angular @ViewChild,Angular 中的 dom 操作

    Angular 中的 dom 操作(原生 js) ngAfterViewInit(){ var boxDom:any=document.getElementById('box'); boxDom.st ...

随机推荐

  1. erlang中字符编码转换(转)

    转自:http://www.thinksaas.cn/group/topic/244329/ 功能说明: erlang中对各种语言的编码支持不足,此代码是使用erlang驱动了著名的iconv编码库来 ...

  2. WPF之DataGrid篇:DataGridComboBoxColumn

    准备数据源   1 准备数据源.基类为Student,数据对象为Student3,数据集为StuList3. END 编辑DataGrid显示列   1 若要填充下拉列表,请首先使用下列选项之一设置 ...

  3. freopen - C/C++文件输入输出利器

    freopen以前经常使用,比较方便,可以当作模板,在中间替换为自己的代码即可使用. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <stdio.h&g ...

  4. php 算法之------------怎样打印出下图

    自己偶尔看到了下图.于是用php打印出下图. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGluZ2ppZ29uZ3Np/font/5a6L5L2T/f ...

  5. 获得手机的ip

    本文转载至 http://blog.csdn.net/showhilllee/article/details/8746114      iosip手机 貌似ASI里获取ip地址的链接不可以了.也曾试过 ...

  6. whl文件下载

    到哪找.whl文件?http://www.lfd.uci.edu/~gohlke/pythonlibs/

  7. 编译EasyDSS rtmp流媒体服务器遇到stray '_239' inprogram,stray '_187' inprogram,stray '_191' inprogram的解决办法

    使用用户提供的交叉编译工具链编译easydss时遇到一个编译错误 stray '\239' inprogram stray '\187' inprogram stray '\191' inprogra ...

  8. html学习笔记(2)-字母大小写转换练习

    主要应用了text-transform属性值: uppercase:所有单词的字母都大写: lowercase:所有单词的字母都小写: capitalize:每个单词的首字母都大写: none:默认值 ...

  9. 云服务器 ECS Linux CentOS 修改内核引导顺序

    由于 CentOS 7 使用 grub2 作为引导程序,所以和 CentOS 6 有所不同,并不是修改 /etc/grub.conf 来修改启动项,需要如下操作: 1. 查看系统内部有多少个内核: c ...

  10. adjA=(detA)A-1

    A–>adjA 连续性 反函数