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

  1. ElementRef, @ViewChild & Renderer

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

  2. [Angular] Difference between ViewChild and ContentChild

    *The children element which are located inside of its template of a component are called *view child ...

  3. Angular ElementRef详解

    一.为什么要用ElementRef Angular 的口号是 - "一套框架,多种平台.同时适用手机与桌面 (One framework.Mobile & desktop.)&quo ...

  4. Angular 2 中的 ViewChild 和 ViewChildren

    https://segmentfault.com/a/1190000008695459 ViewChild ViewChild 是属性装饰器,用来从模板视图中获取匹配的元素.视图查询在 ngAfter ...

  5. Angular 2 ViewChild & ViewChildren

    一.ViewChild ViewChild 是属性装饰器,用来从模板视图中获取匹配的元素.视图查询在 ngAfterViewInit 钩子函数调用前完成,因此在 ngAfterViewInit 钩子函 ...

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

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

  7. angular父组件通过@ViewChild 主动获取子组 件的数据和方法

    1.调用子组件给子组件定义一个名称 <app-footer #footerChild></app-footer> 2. 引入 ViewChild import { Compon ...

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

  9. 开发Angular库的简单指导(译)

    1. 最近工作上用到Angular,需要查阅一些英文资料,虽然英文非常烂,但是种种原因又不得不硬着头皮上,只是每次看英文都很费力,因此决定将一些比较重要的特别是需要反复阅读的资料翻译一下,以节约再次阅 ...

随机推荐

  1. html --- ajax --- javascript --- 简单的封装

    Ajax的简单封装 Ajax的全称是AsynchronousJavaScriptAndXML 如有疑问请参考:http://zh.wikipedia.org/zh-cn/AJAX 以及传智播客的视频教 ...

  2. JavaScript Type Conversion

    Data Types 5 Data Types string, number, boolean, object, function 3 Object Types object, array, date ...

  3. 25个CSS3 渐变和动画效果教程

    随着最新版CSS3渐变和动画功能发布,Web开发者在开发的过程中有了更多的选择.实际上,已经有了一些替代的技术,目的都是使网站的建设变得简易,高效和快速.不过CSS3所提供的渐变功能有着显著的优点,特 ...

  4. Ubuntu 下一个可用的音乐播放器

    参考:http://www.pairsdoll.com/install-audacious-music-palyer-in-ubuntu.html/ 方法:打开terminal,sudo apt-ge ...

  5. 在mac上搭建python环境

    原文出处:http://blog.justbilt.com/2014/07/02/setup_python_on_mac/ 这两天重新搞了下python的环境,发现好多地方还是容易忘记,因此有了这篇文 ...

  6. brew 更新

    更新: brew update brew update —system 安装, 如:brew install unrar 卸载, 如:brew uninstall unrar

  7. deep learning 的java库

    deepLearning4j: https://github.com/deeplearning4j/libnd4j/issues/208                       deepLearn ...

  8. MySQL/MariaDB/Percona数据库升级脚本

    MySQL/MariaDB/Percona数据库升级脚本截取<OneinStack>中upgrade_db.sh, 一般情况下不建议升级数据库版本,该脚本专提供给各位版本控们.为防止大版本 ...

  9. Xtrabackup之innobackupex备份恢复详解(转)

    add by zhj:对于Xtrabackup2.2来说,已经解决了本文结尾提到的那个bug,当使用--copy-back时,同时加--force-non-empty-directories 即可.这 ...

  10. Styling FX Buttons with CSS

    http://fxexperience.com/2011/12/styling-fx-buttons-with-css/ ——————————————————————————————————————— ...