[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,需要查阅一些英文资料,虽然英文非常烂,但是种种原因又不得不硬着头皮上,只是每次看英文都很费力,因此决定将一些比较重要的特别是需要反复阅读的资料翻译一下,以节约再次阅 ...
随机推荐
- (转载)OC学习篇之---Foundation框架中的NSObject对象
前一篇文章讲到了OC中的代理模式,而且前几篇文章就介绍了OC中的类相关知识,从这篇文章开始我们开始介绍Foundation框架. OC中的Foundation框架是系统提供了,他就相当于是系统的一套a ...
- AI钻石风格logo教程
最终图像 与往常一样,这是我们要创建的最终图像: Step 1 按Ctrl+ N创建新文档.从单位下拉菜单中选择像素,在宽度和高度框中输入600,然后单击高级按钮.选择RGB,屏幕(72 PPI),并 ...
- WS之cxf与spring整合2
在action中加入webservice
- IOS 异步加载图片
#import <Foundation/Foundation.h> #import "StringUtils.h" @interface ImageManager : ...
- Hbase学习记录(2)| Shell操作
查看表结构 describe '表名' 查看版本 get '表名','zhangsan'{COLUMN=>'info:age',VERSIONS=>3} 删除整行 deleteall '表 ...
- 通过网络方式安装linux的五种方法
在线观看:http://video.sina.com.cn/v/b/43086503-1443650204.html http://video.sina.com.cn/v/b/43095530-144 ...
- emWin(ucGui)的Edit控件退格处理方法 worldsing
在enWin(ucGui)中EDIT控件在数值模式(十进制/十六进制/二进制/浮点数)下编辑是,无法使用BackSpace键进行退格,主要涉及到的函数有: EDIT_SetBinMode() EDIT ...
- spring中文乱码过滤器
中文乱码过滤器 在您通过表单向服务器提交数据时,一个经典的问题就是中文乱码问题.虽然我们所有的 JSP 文件和页面编码格式都采用 UTF-8,但这个问题还是会出现.解决的办法很简单,我们只需要在 we ...
- maven profile动态选择配置文件
一.背景 在开发过程中,我们的软件会面对不同的运行环境,比如开发环境.测试环境.生产环境,而我们的软件在不同的环境中,有的配置可能会不一样,比如数据源配置.日志文件配置.以及一些软件运行过程中的基本配 ...
- Kafka架构设计:分布式发布订阅消息系统
[http://www.oschina.net/translate/kafka-design](较长:很详细的讲解) [我们为什么要搭建该系统]用作LinkedIn的活动流(activity stre ...