[Angular 2] ElementRef, @ViewChild & Renderer
ElementRef:
In Angular2 Doc, it suggest to "avoid" using ElementRef. It access DOM directly, can easily be attacked.
import {Component, OnInit, ViewChild, Renderer, ElementRef} from '@angular/core';
@Component({
moduleId: module.id,
selector: 'widget-three',
template: `<input type="text" #inputRef/>`
})
export class WidgetThree {
constructor(private elm: ElementRef) {
console.log("elm:", this.elm)
}
}

If we log out the ElementRef, we can see, it refer to host element.
Renderer:
In the doc, it also suggest, if you want to change some dom prop, use Renderer instead. ElementRef can be just a reference to access native element object.
import { Directive, ElementRef, Input, Renderer } from '@angular/core';
@Directive({ selector: '[myHighlight]' })
export class HighlightDirective {
constructor(el: ElementRef, renderer: Renderer) {
renderer.setElementStyle(el.nativeElement, 'backgroundColor', 'yellow');
}
}
This will set the host element background as yellow.
@ViewChild():
Access Child element by Ref or Class Object.
import {Component, OnInit, ViewChild, Renderer} from '@angular/core';
@Component({
moduleId: module.id,
selector: 'widget-three',
template: `<input type="text" #inputRef/>`
})
export class WidgetThree {
@ViewChild('inputRef') input;
constructor(private renderer: Renderer) {
}
ngAfterViewInit(){
this.renderer.invokeElementMethod(
this.input.nativeElement,
'focus',
[]
);
}
}
Here we have a ref "inputRef", we use ref to access input element.
'invokeElementMethod' will call the 'focus' method the the input nativeElement which should be:
this.input.nativeElement.focus()
But the risk is on mobile it might have different method to focus on input, 'invokeElementMethod' can safely help us to call the method .
[Angular 2] ElementRef, @ViewChild & Renderer的更多相关文章
- ElementRef, @ViewChild & Renderer
ElementRef: In Angular2 Doc, it suggest to "avoid" using ElementRef. It access DOM directl ...
- [Angular] Difference between ViewChild and ContentChild
*The children element which are located inside of its template of a component are called *view child ...
- Angular ElementRef详解
一.为什么要用ElementRef Angular 的口号是 - "一套框架,多种平台.同时适用手机与桌面 (One framework.Mobile & desktop.)&quo ...
- Angular 2 中的 ViewChild 和 ViewChildren
https://segmentfault.com/a/1190000008695459 ViewChild ViewChild 是属性装饰器,用来从模板视图中获取匹配的元素.视图查询在 ngAfter ...
- Angular 2 ViewChild & ViewChildren
一.ViewChild ViewChild 是属性装饰器,用来从模板视图中获取匹配的元素.视图查询在 ngAfterViewInit 钩子函数调用前完成,因此在 ngAfterViewInit 钩子函 ...
- angular ViewChild ContentChild 系列的查询参数
官方说明 官方文档 在调用 NgAfterViewInit 回调函数之前就会设置这些视图查询. 元数据属性: selector - 用于查询的指令类型或名字. read - 从查询到的元素中读取另一个 ...
- angular父组件通过@ViewChild 主动获取子组 件的数据和方法
1.调用子组件给子组件定义一个名称 <app-footer #footerChild></app-footer> 2. 引入 ViewChild import { Compon ...
- [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 ...
- 开发Angular库的简单指导(译)
1. 最近工作上用到Angular,需要查阅一些英文资料,虽然英文非常烂,但是种种原因又不得不硬着头皮上,只是每次看英文都很费力,因此决定将一些比较重要的特别是需要反复阅读的资料翻译一下,以节约再次阅 ...
随机推荐
- First step of using junit---------Software Testing Lab 1---2016.03.18
1. Install junit a) Download “junit.jar” b) In eclipse, Windows->Preferences->Java-& ...
- Python的OO思想
想当年大二的时候,在学校学习Java, 最牛逼的OO思想,用了3页纸就讲完了,还是清华大学出版社的呢. 后来全凭自己啃视频,啃代码才搞懂什么叫做OO. 现在学习Python,就用自己的方式,好好学习一 ...
- 【125】固定IP的电脑配置无线路由
标题所提到的情况即为有些电脑是用静态IP上网的,与普通的宽带连接稍微有些不同,例如我的电脑的静态IP设置是这样的: 只有按照上面的设置才可以正常上网,因此在配置无线路由器的时候也要用到上面的内容,废话 ...
- vim切换buffer
[vim切换buffer] 命令 ls 可查看当前已打开的buffer 命令 b num 可切换buffer (num为buffer list中的编号) 其它命令: :bn -- buffer列表中下 ...
- JavaScript面向对象简介
JavaScript面向对象简介 @(编程) [TOC] 1. 命名空间 命名空间是一个容器,它允许开发人员在一个独特的,特定于应用程序的名称下捆绑所有的功能. 在JavaScript中,命名空间只是 ...
- hdu 2199 Can you solve this equation?(高精度二分)
http://acm.hdu.edu.cn/howproblem.php?pid=2199 Can you solve this equation? Time Limit: 2000/1000 MS ...
- 将表A的数据复制到表B,以及关于主表和子表的删除办法
如果表A的数据结构和表B的数据结构是一样的,字段名字可以不用相同,但是对应的数据类型是一样的 这样的情况下可以用如下的方式实现将表A的数据复制到表B INSERT INTO #TEMP2 SELECT ...
- 关于mysql存储过程的definer的问题
由于对mysql了解不够透彻,导致对definer问题查了好久才解决问题 记录自己的一些理解! 问题描述: 在数据库写,为一个表写了一个触发器,此触发器调用一个存储过程:由公司写的一个c程序自动往该表 ...
- Decode放在where条件后的新用法
今天遇到一种特殊情况的查询,在查询某表时,要通过判断其中一个字段的值再用其他字段作为条件查询,比如有3个字段 columnA,columnBm,columnC,columnA的值由两个——分别是0和1 ...
- plsql配置连接远程数据库
一.首先安装PL/SQL Developer 下载地址:https://yunpan.cn/cM3njKpfK8MnT 访问密码 996a 二.再安装instantclient_11_2 下载地址:h ...