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 .
转自:http://www.cnblogs.com/Answer1215/p/5898545.html
ElementRef, @ViewChild & Renderer的更多相关文章
- [Angular 2] ElementRef, @ViewChild & Renderer
ElementRef: In Angular2 Doc, it suggest to "avoid" using ElementRef. It access DOM directl ...
- Angular ElementRef详解
一.为什么要用ElementRef Angular 的口号是 - "一套框架,多种平台.同时适用手机与桌面 (One framework.Mobile & desktop.)&quo ...
- angular ViewChild ContentChild 系列的查询参数
官方说明 官方文档 在调用 NgAfterViewInit 回调函数之前就会设置这些视图查询. 元数据属性: selector - 用于查询的指令类型或名字. read - 从查询到的元素中读取另一个 ...
- Angular 2 中的 ViewChild 和 ViewChildren
https://segmentfault.com/a/1190000008695459 ViewChild ViewChild 是属性装饰器,用来从模板视图中获取匹配的元素.视图查询在 ngAfter ...
- Angular 2 ViewChild & ViewChildren
一.ViewChild ViewChild 是属性装饰器,用来从模板视图中获取匹配的元素.视图查询在 ngAfterViewInit 钩子函数调用前完成,因此在 ngAfterViewInit 钩子函 ...
- [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 ...
- [nodejs,expressjs,angularjs2] LOL英雄列表数据抓取及查询显示应用
新手练习,尝试使用angularjs2 [angularjs2 数据绑定,监听数据变化自动修改相应dom值,非常方便好用,但与传统js(jquery)的使用方法会很不同,Dom操作也不太习惯] 应用效 ...
- 开发Angular库的简单指导(译)
1. 最近工作上用到Angular,需要查阅一些英文资料,虽然英文非常烂,但是种种原因又不得不硬着头皮上,只是每次看英文都很费力,因此决定将一些比较重要的特别是需要反复阅读的资料翻译一下,以节约再次阅 ...
- 利用Angular实现多团队模块化SPA开发框架
0.前言 当一个公司有多个开发团队时,我们可能会遇到这样一些问题: 技术选项杂乱,大家各玩各 业务重复度高,各种通用api,登录注销,权限管理都需要重复实现(甚至一个团队都需要重复实现) 业务壁垒,业 ...
随机推荐
- Jquery datepicker的使用
1. 设定初始日期 $("#<%=txtStart.ClientID %>").datepicker("setDate", start); 2. 设 ...
- iOS 应用发布
本文转载至 http://blog.csdn.net/ysy441088327/article/details/7833579 苹果为广大的开发者提供了一个很好的应用生态环境 参考资料: 1:如何向 ...
- js中scrollLeft、scrollWidth、offsetTop等相关位置属性的理解(转)
1.常见的事件位置属性 e.pageX——相对整个页面的坐标 注意:IE6.IE7.IE8无该属性 e.layerX——相对当前坐标系的border左上角开始的坐标 注意:在opera.IE6.IE7 ...
- Java 学习 day06
01-面向对象(Static关键字) package myFirstCode; /* 静态:static. 用法:是一个修饰符,用于修饰成员(成员变量,成员函数) 当成员被静态修饰后,就多了一个调用方 ...
- JAVA解析XML之SAX方式
JAVA解析XML之SAX方式 SAX解析xml步骤 通过SAXParseFactory的静态newInstance()方法获取SAXParserFactory实例factory 通过SAXParse ...
- HTML 学习笔记 JQuery(animation)
动画效果也是JQuery库吸引人的地方,通过JQuery的动画方法,能够轻松的为网页天假非常紧菜的视觉效果. show()方法和hide()方法 show()方法和hide()方法是JQuery中最基 ...
- 已知段地址,求CPU寻址范围
已知段地址为0001H,仅通过变化偏移地址寻址,则CPU的寻址范围是? 物理地址 = 段地址×16 + 偏移地址 所以物理地址的范围是[16×1H+0H, 16×1H+FFFFH] 也就是[10H×1 ...
- cron表达式(转)
原文地址:http://www.cnblogs.com/linjiqin/archive/2013/07/08/3178452.html Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或 ...
- join中级篇---------hash join & merge join & nested loop Join
嵌套循环连接(Nested Loop Join) 循环嵌套连接是最基本的连接,正如其名所示那样,需要进行循环嵌套,嵌套循环是三种方式中唯一支持不等式连接的方式,这种连接方式的过程可以简单的用下图展示: ...
- 在Linux下搭建我的世界(Minecraft)服务器
最近薅了百度云双12的羊毛,1核2G一年150.突然想起以前大学整个宿舍通宵开黑挖泥土的岁月,所以刚好趁着这台服务器,打算自己搭建一个我的世界服务器,重温一下以前的感觉. 超详细Linux搭建Java ...