angular5 @viewChild @ContentChild ElementRef renderer2
@viewChild
作用一:选择组件内节点
<!--视图 -->
<div #mydiv><input></div>
// 选择
@ViewChild('mydiv') mydiv: ElementRef
// 返回原生节点
let el = this.mydiv.nativeElement //
// 使用原生方法
let ipt = el.querySelector('input')
作用二:选择子组件可调用自组件内的函数
子组件:
@Component({ selector: 'user-profile' })
export class UserProfile {
constructor() {}
sendData() { //send data }
}
当前组件
import { Component, ViewChild } from '@angular/core';
import { UserProfile } from '../user-profile';
@Component({ template: '<user-profile (click)="update()"></user-profile>', })
export class MasterPage {
// ViewChild takes a class type or a reference name string.
// Here we are using the type
@ViewChild(UserProfile) userProfile: UserProfile
constructor() { } ngAfterViewInit() {
// After the view is initialized,
this.userProfile will be available this.update();
}
update() {
this.userProfile.sendData();
}
}
@ViewChild @ContentChild @ViewChildren @ContentChildren 又是什么
@ViewChild 选择组件模板内的节点
@ContentChild 选择当前组件引用的子组件 @ContentChild(组件名)
@ViewChildren 和 @ContentChildren 则为对应的复数
import { Component, ContentChild, AfterContentInit } from '@angular/core';
import { ChildComponent } from './child.component';
@Component({
selector: 'exe-parent',
template: `
<p>Parent Component</p>
<ng-content></ng-content>
`
})
export class ParentComponent implements AfterContentInit {
@ContentChild(ChildComponent)
childCmp: ChildComponent;
ngAfterContentInit() {
console.dir(this.childCmp);
}
}
import { Component } from '@angular/core';
@Component({
selector: 'exe-child',
template: `
<p>Child Component</p>
`
})
export class ChildComponent {
name: string = 'child-component';
}
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<h4>Welcome to Angular World</h4>
<exe-parent>
<exe-child></exe-child>
</exe-parent>
`,
})
export class AppComponent { }
ContentChild 与 ViewChild 的异同点
相同点
都是属性装饰器
都有对应的复数形式装饰器:ContentChildren、ViewChildren
都支持 Type<any>|Function|string 类型的选择器
不同点
ContentChild 用来从通过 Content Projection 方式 (ng-content) 设置的视图中获取匹配的元素
ViewChild 用来从模板视图中获取匹配的元素
在父组件的 ngAfterContentInit 生命周期钩子中才能成功获取通过 ContentChild 查询的元素
在父组件的 ngAfterViewInit 生命周期钩子中才能成功获取通过 ViewChild 查询的元素
renderer2
// 添加类
this.renderer2.addClass(el, 'active')
// 移除了类
this.renderer2.removeClass(el, 'active')
// 设置样式
this.renderer2.setStyle(el, 'height', '10px')
// 移除样式
this.renderer2.removeStyle(el, 'height')
// 设置属性
this.renderer2.setAttribute(el, 'data-id', 'id')
// 移除属性
this.renderer2.removeAttribute(el, 'data-id')
// 设置值
this.renderer2.setValue(ipt, 'some str')
// 监听事件
this.renderer2.listen(el, 'click', (e)=>{console.log(e)}) //el|'window'|'document'|'body'
// 其他类似
createElement 创建元素
createComment 动态创建组件
createText 创建文本节点
destroyNode 销毁节点
appendChild 插入子节点
insertBefore (parent: any, newChild: any, refChild: any): void
removeChild(parent: any, oldChild: any): void 移除子元素
selectRootElement(selectorOrNode: string|any): any
parentNode(node: any): any
nextSibling(node: any): any
angular5 @viewChild @ContentChild ElementRef renderer2的更多相关文章
- angular ViewChild ContentChild 系列的查询参数
官方说明 官方文档 在调用 NgAfterViewInit 回调函数之前就会设置这些视图查询. 元数据属性: selector - 用于查询的指令类型或名字. read - 从查询到的元素中读取另一个 ...
- angular学习第1步
#### 最专业,最全面的angular的学习文档 https://www.jianshu.com/p/f0f81a63cbcb ### https://www.cnblogs.com/xiaowei ...
- 【angular5项目积累总结】优秀组件以及应用实例
1.手机端 图片预览组件 组件:sideshow 效果图:(预览图全屏 且可以左右移动) code: <div class="row ui-app-s ...
- [Angular 2] ElementRef, @ViewChild & Renderer
ElementRef: In Angular2 Doc, it suggest to "avoid" using ElementRef. It access DOM directl ...
- ElementRef, @ViewChild & Renderer
ElementRef: In Angular2 Doc, it suggest to "avoid" using ElementRef. It access DOM directl ...
- Angular开发实践(七): 跨平台操作DOM及渲染器Renderer2
在<Angular开发实践(六):服务端渲染>这篇文章的最后,我们也提到了在服务端渲染中需要牢记的几件事件,其中就包括不要使用window. document. navigator等浏览器 ...
- [ionic3.x开发记录]参考ionic的float-label动效,写一个项目内通用的input组件,易扩展
上图: module: import {NgModule} from "@angular/core"; import {CommonModule} from "@angu ...
- Angular8稳定版修改概述
在今天早些时候Angular团队发布了8.0.0稳定版.其实早在NgConf 2019大会上,演讲者就已经提及了从工具到差分加载的许多内容以及更多令人敬畏的功能.下面是我对8.0.0一些新功能的简单介 ...
- [Angular 2] Set Properties on Dynamically Created Angular 2 Components
When you generate Angular 2 components, you’re still able to access the component instance to set pr ...
随机推荐
- Linux 命令 which whereis locate find
which: 查询某指令的完整路径 $ which [-a] command -a: 将所有在PATH目录中可以找到的指令均列出. 注意:只搜索PATH下的路径. whereis: 只搜索几个特定目录 ...
- Presto实战
一.Presto简介 1.PRESTO是什么? Presto是一个开源的分布式SQL查询引擎,适用于交互式分析查询,数据量支持GB到PB字节. Presto的设计和编写完全是为了解决像Facebook ...
- RabbitMQ的消息确认机制
一:确认种类 RabbitMQ的消息确认有两种. 一种是消息发送确认.这种是用来确认生产者将消息发送给交换器,交换器传递给队列的过程中,消息是否成功投递.发送确认分为两步,一是确认是否到达交换器,二是 ...
- maven工程下添加oracle驱动包
maven工程手动安装oracle驱动包到本地仓库 下载ojdbc.jar包 这个当然是最好去官网下载了.http://mvnrepository.com这个仓库上有,具体页面地址为: http:// ...
- 运行pytorch代码遇到的error解决办法
1.no CUDA-capable device is detected 首先考虑的是cuda的驱动问题,查看gpu显示是否正常,然后更新最新的cuda驱动: 第二个考虑的是cuda设备的默认参数是否 ...
- 为VisualStudio2017添加bits/stdc++.h
在算法编程中经常有人只写一个头文件"bits/stdc++.h" 其实这个是很多头文件的集合,写了它后相当于包含了所有常用的C++头文件,可是需要注意的是并不是所有的OJ系统都支持 ...
- CentOS 7 之 Systemd 入门教程:实战篇
开机启动对于那些支持 Systemd 的软件,安装的时候,会自动在/usr/lib/systemd/system目录添加一个配置文件. 如果你想让该软件开机启动,就执行下面的命令(以httpd.ser ...
- 小程序canvas生成海报保存至手机相册
小程序canvas画图保存至手机相册 (1)可直接展示生成的海报 .因手机分辨率不同可能导致生成的海报会有细微差别,这里隐藏canvas海报,页面正常设置海报样式保存时保存隐藏的canvas海报 (2 ...
- CSRF(Cross Site Request Forgery, 跨站域请求伪造)
CSRF(Cross Site Request Forgery, 跨站域请求伪造) CSRF 背景与介绍 CSRF(Cross Site Request Forgery, 跨站域请求伪造)是一种网络的 ...
- PHP函数------parse_ini_file()
1.parse_ini_file()函数用于解析一个配置文件,并以数组的形式返回其中的设置. 举例说明:group.ini文件,文件内容如下: 0 = "Hleducation" ...