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

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

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

  2. angular学习第1步

    #### 最专业,最全面的angular的学习文档 https://www.jianshu.com/p/f0f81a63cbcb ### https://www.cnblogs.com/xiaowei ...

  3. 【angular5项目积累总结】优秀组件以及应用实例

    1.手机端 图片预览组件 组件:sideshow 效果图:(预览图全屏 且可以左右移动)                  code: <div class="row ui-app-s ...

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

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

  5. ElementRef, @ViewChild & Renderer

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

  6. Angular开发实践(七): 跨平台操作DOM及渲染器Renderer2

    在<Angular开发实践(六):服务端渲染>这篇文章的最后,我们也提到了在服务端渲染中需要牢记的几件事件,其中就包括不要使用window. document. navigator等浏览器 ...

  7. [ionic3.x开发记录]参考ionic的float-label动效,写一个项目内通用的input组件,易扩展

    上图: module: import {NgModule} from "@angular/core"; import {CommonModule} from "@angu ...

  8. Angular8稳定版修改概述

    在今天早些时候Angular团队发布了8.0.0稳定版.其实早在NgConf 2019大会上,演讲者就已经提及了从工具到差分加载的许多内容以及更多令人敬畏的功能.下面是我对8.0.0一些新功能的简单介 ...

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

随机推荐

  1. 怎样把Word文档导入Excel表格

    Word是现在办公中的基础文件格式了,很多的内容我们都通过Word来进行编辑,那么当我们需要将Word文档里的信息导入到Excel里面的时候,我们应该怎样做呢?下面我们就一起来看一下吧. 操作步骤: ...

  2. Linux学习5-CentOS安装Python3.6环境和pip3

    前言 centos7 自带有 python,但是却是 python2 版本的 python,如果你想安装个python3怎么办呢? 如果直接删除python2的话,可能会引起其他的问题,因为有些东西是 ...

  3. soapui调用redis,获取短信验证码

    1.首先,调用redis需要引入redis的jar包,放入到soapui指定目录中,例如我的目录D:\Program Files\SmartBear\SoapUI-Pro-5.1.2\bin\ext ...

  4. rimraf 跨平台删除文件

    利用npm script 来删除文件, "scripts": { "clear": "rm -rf dist" } 但存在一个问题,remo ...

  5. python_函数式编程

    函数式编程是一种编程范式 (而面向过程编程和面向对象编程也都是编程范式).在面向过程编程中,我们见到过函数(function):在面向对象编程中,我们见过对象(object).函数和对象的根本目的是以 ...

  6. DevOps“五宗罪”,这样向DevOps过渡注定会失败

    云计算提供的速度响应.敏捷性和规模效应,契合了如今不断变化的数字商业环境.企业基于最新的IT技术,重构IT架构,加速产品创新和服务交付的速度,从而提高运营效率和市场占有. 不过,企业IT管理者在利用云 ...

  7. 虚拟机VM三种网络连接方式说明

  8. IRC 打字交流

    kali 里面用 apt-get install weechat 安装完成后,输入 weechat 命令就能启动客户端了 要想使用 IRC,就需要先连接一个 irc 服务器,选择了大名鼎鼎的 chat ...

  9. Tomcat增加Context配置不带项目名访问导致启动的时候项目加载两次

    eclipse发布web应用至tomcat,默认方式下访问该项目是需要带项目名称的,例http://localhost:8080/myapp/.现在需要改成这样访问http://localhost.修 ...

  10. 第一篇——Struts2的工作原理及HelloWorld简单实现

    Struts2工作原理: 一个请求在Struts框架中的处理步骤: 1.客户端初始化一个指向Servlet容器(例如Tomcat)的请求: 2.这个请求经过一系列的过滤器(Filter): 3.接着F ...