viewchild

// 使用方法
git clone https://git.oschina.net/mumu-osc/learn-component.git
cd learn-component
git pull origin viewchild
npm install
ng serve
<!-- test-view-child.component.html -->
<div class="panel panel-primary">
<div class="panel-heading">父组件</div>
<div class="panel-body">
<child-one></child-one>
<child-one></child-one>
<child-one></child-one>
<child-one></child-one>
</div>
</div>
// test-view-child.component.ts
import { Component, OnInit, ViewChild, ViewChildren, QueryList } from '@angular/core';
import { ChildOneComponent } from './child-one/child-one.component'; @Component({
selector: 'test-view-child',
templateUrl: './test-view-child.component.html',
styleUrls: ['./test-view-child.component.scss']
})
export class TestViewChildComponent implements OnInit {
// @ViewChild(ChildOneComponent)
// childOne:ChildOneComponent;
@ViewChildren(ChildOneComponent)
children:QueryList<ChildOneComponent>; constructor() { } ngOnInit() {
} ngAfterViewInit():void{
// console.log(this.childOne);
this.children.forEach((item)=>{
console.log(item);
//动态监听子组件的事件
item.helloEvent.subscribe((data)=>{
console.log(data);
});
});
}
}
// child-one.component.ts
import { Component, OnInit, Output, EventEmitter } from '@angular/core'; @Component({
selector: 'child-one',
templateUrl: './child-one.component.html',
styleUrls: ['./child-one.component.scss']
})
export class ChildOneComponent implements OnInit {
@Output()
helloEvent:EventEmitter<string>=new EventEmitter<string>(); constructor() { } ngOnInit() {
} public sayHello():void{
this.helloEvent.emit("hello...");
}
}

什么是ViewChild

从上面的代码可以看出viewchild是为了父组件可以获取字组件,进行计数、调用字组件内部方法等等功能所提供的机制。。。。

具体用法:比如,可以在轮播图组件中,进行获取所插入图片的数量等,从而实现一个通用的轮播图组件

Angular ViewChild的更多相关文章

  1. Angular Viewchild undefined

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

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

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

  3. angular ViewChild ContentChild 系列的查询参数

    官方说明 官方文档 在调用 NgAfterViewInit 回调函数之前就会设置这些视图查询. 元数据属性: selector - 用于查询的指令类型或名字. read - 从查询到的元素中读取另一个 ...

  4. [Angular] @ViewChild read custom directive and exportAs

    For example we have a component: <Card ></Card> And a driective: <Card highlighted> ...

  5. [Angular] ViewChild 'read' option

    It is not clear in the Docs about {read: xx} option for @ViewChild. Based on the Source code, @ViewC ...

  6. [Angular] @ViewChild and template #refs to get Element Ref

    We can use @ViewChild with component: @ViewChild(AuthMessageComponent) message: AuthMessageComponent ...

  7. Angular ViewChild & ViewChildren

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

  8. 如何把一个vue组件改为ionic/angular组件

    同是mvvm框架,他们之间是很相似的,如何你已经熟悉其中的一个,那么另一个也就基本上也就会的差不多了. 一.动态属性.值.事件绑定 vue中使用v-bind:或者之间分号:进行绑定 ng中左括号[]进 ...

  9. [Angular 2] ElementRef, @ViewChild & Renderer

    ElementRef: In Angular2 Doc, it suggest to "avoid" using ElementRef. It access DOM directl ...

随机推荐

  1. Linux程序调试GDB——数据查看

    查看栈信息 当程序被停住了,首先要确认的就是程序是在哪儿被断住的.这个一般是通过查看调用栈信息来看的.在gdb中,查看调用栈的命令是backtrace,可以简写为bt. (gdb) bt    #0 ...

  2. PHP-数据库长连接mysql_pconnect的细节

    PHP的MySQL持久化连接,美好的目标,却拥有糟糕的口碑,往往令人敬而远之.这到底是为啥么.近距离观察后发现,这家伙也不容易啊,要看Apache的脸色,还得听MySQL指挥. 对于作为Apache模 ...

  3. Python的ipython的安装

    IPython是Python 的原生交互式 shell 的增强版,可以完成许多不同寻常的任务,比如帮助实现并行化计算:主要使用它提供的交互性帮助,比如代码着色.改进了的命令行回调.制表符完成.宏功能以 ...

  4. 学习C#——性能计数器

    写在前面: 作为Web应用开发前线的一枚小兵,每看到“性能”一词总有种要亮瞎眼的感觉,说到“性能”那就不能不提“数据”,在程序猿.攻城师中不是流行这样一句话吗?“无图无真相”,谁要说谁开发的应用性能有 ...

  5. 了解C#文件操作

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  6. 如何用nodejs 开发一个命令行交互工具

    参考地址1 参考地址2 一.npm package.json bin 1.package.json { "name": "test", "versio ...

  7. HTTP长连接?短连接?长轮询?短轮询?

    错觉与突然的察觉 大多数人都知道HTTP1.0不支持长连接,知道HTTP1.1支持长连接. 这是业界的一个常识. 然而这样的描述导致了一些不做网络底层开发的开发者都下意识的认为HTTP1.1是一个可以 ...

  8. Git Manual / Git使用手册 / Git, GitLab, Git Bash, TortoiseGit (建议全文复制到Word文档中通过导航窗格查看)

    Git使用手册 目录 1     引言 2     Git.GitLab简介 2.1      Git 2.2      GitLab 2.3      Git基本概念 3     运行环境 4    ...

  9. 源码分析:Java堆的创建

    虚拟机在内存中申请一片区域,由虚拟机自动管理,用来满足应用程序对象分配的空间需求,即堆空间. 由于程序运行的局部特性,程序创建的大多数对象都具有非常短的生命周期,而程序也会创建一些生命周期特别长的对象 ...

  10. Selenium - Webdriver API /ActionChains API

    一.控制浏览器 1.1 控制浏览器窗口大小 # 获取当前浏览器的大小  driver.get_window_size() # 通过像素设置浏览器的大小  driver.set_window_size( ...