Angular ViewChild
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的更多相关文章
- 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 ...
- angular ViewChild ContentChild 系列的查询参数
官方说明 官方文档 在调用 NgAfterViewInit 回调函数之前就会设置这些视图查询. 元数据属性: selector - 用于查询的指令类型或名字. read - 从查询到的元素中读取另一个 ...
- [Angular] @ViewChild read custom directive and exportAs
For example we have a component: <Card ></Card> And a driective: <Card highlighted> ...
- [Angular] ViewChild 'read' option
It is not clear in the Docs about {read: xx} option for @ViewChild. Based on the Source code, @ViewC ...
- [Angular] @ViewChild and template #refs to get Element Ref
We can use @ViewChild with component: @ViewChild(AuthMessageComponent) message: AuthMessageComponent ...
- Angular ViewChild & ViewChildren
基础 ViewChild ViewChild 装饰器用于获取模板视图中的元素或直接调用其组件中的方法.它支持 Type 类型或 string 类型的选择器,同时支持设置 read 查询条件,以获取不同 ...
- 如何把一个vue组件改为ionic/angular组件
同是mvvm框架,他们之间是很相似的,如何你已经熟悉其中的一个,那么另一个也就基本上也就会的差不多了. 一.动态属性.值.事件绑定 vue中使用v-bind:或者之间分号:进行绑定 ng中左括号[]进 ...
- [Angular 2] ElementRef, @ViewChild & Renderer
ElementRef: In Angular2 Doc, it suggest to "avoid" using ElementRef. It access DOM directl ...
随机推荐
- 转:Socket常用选项
功能描述 获取或者设置与某个套接字关联的选 项.选项可能存在于多层协议中,它们总会出现在最上面的套接字层.当操作套接字选项时,选项位于的层和选项的名称必须给出.为了操作套接字层的选项,应该 将层的值指 ...
- Oracle基础学习2--Oracle登录与三种验证机制
首先,Oracle安装完毕有三个默认用户 Ø Sys:数据库对象的拥有者.权限最高.password在安装的时候(口令管理)能够改变 Ø System:数据库管理员,password为manage ...
- 【Redis】redis+php处理高并发,很好的教程||附上 php的文件锁
链接至:http://blog.csdn.net/nuli888/article/details/51865401 很好的教程,其中redis+php有点小问题. 附上php文件锁: $fp = fo ...
- 一个优秀的Javascript框架--Prototype解说
http://www.cnblogs.com/meil/archive/2007/04/24/724200.html Prototype.js 是Ruby On Rails的副产品, Javasc ...
- (Windows Maven项目)Redis数据库的安装和操作实现
Redis是一个内存数据库,他会把你写入当中的数据缓存到内存中,之后会周期性的往磁盘中写入.这篇文章中介绍的是在Windows环境下利用Maven工具编译运行Java文件实现Red ...
- 深入浅出ObjC之消息 (转)
在入门级别的ObjC 教程中,我们常对从C++或Java 或其他面向对象语言转过来的程序员说,ObjC 中的方法调用(ObjC中的术语为消息)跟其他语言中的方法调用差不多,只是形式有些不同而已. 譬如 ...
- max_int和-1
#include <stdio.h> int main(int argc, char *argv[]) { unsigned ; ) printf("umax:%u == -1\ ...
- atitit.提升软件开发的生产力关健点-------大型开发工具最关健
atitit.提升软件开发的生产力关健点-------大型开发工具最关健 1. 可以创作出更好的工具遍历自己 1 2. 大型工具包括哪些方面 2 2.1. ide 2 2.2. dsl 2 2.3. ...
- AppModify修改app.config
public class AppModify { /// <summary> /// 依据连接串名字connectionName返回数据连接字符串 /// </summary> ...
- Getting Started with Amazon EC2 (1 year free AWS VPS web hosting)
from: http://blog.coolaj86.com/articles/getting-started-with-amazon-ec2-1-year-free-aws-vps-web-host ...