[Angular] @ContentChild with Directive ref
For example you have a component, which take a trasclude input element:
<au-fa-input id="password-field" icon="lock" >
<input placeholder="Password" class="test-class">
</au-fa-input>
There is many ways to get ElementRef of the input, for example using Reference:
<au-fa-input id="password-field" icon="lock" >
<input #input placeholder="Password" class="test-class">
</au-fa-input>
We add a #input ref, so that inside component, we can using @ContentChild and AfterContentInit lifecycle:
export class AuFaInputComponent implements AfterContentInit{
@Input()
icon: string;
@ContentChild('input')
input: HTMLInputElement;
ngAfterContentInit () {
console.log("input", this.input);
}
This approach works fine, but it requires us to put a element ref onto the input field.
One way to avoid putting a reference is creating a directive ref, and we can add common functionalitiy to it, such as focus and blur events:
import {Directive, HostListener} from '@angular/core';
@Directive({
selector: 'au-fa-input input'
})
export class InputRefDirective {
focus = false;
@HostListener('focus')
isFocus() {
console.log("now focus");
this.focus = true;
}
@HostListener('blur')
isBlur() {
console.log("now blur");
this.focus = false;
}
}
And we our component, we need to change @ContentChild:
@ContentChild(InputRefDirective)
input: InputRefDirective;
So for now, in html, we don't need #input any more:
<au-fa-input id="password-field" icon="lock" >
<input placeholder="Password" class="test-class">
</au-fa-input>
Now, let's say if we want to add some styling base on whether input field is focused or not.
First, some css class:
// component
:host(.input-focus) {
border-color: #4D90FE;
-webkit-box-shadow: 0 0 5px #4D90FE;
box-shadow: 0 0 5px #4D90FE;
}
Use the function form to apply host styles conditionally by including another selector inside parentheses after :host. So here, we check if .input-focus is present on the host element, then we apply the styling, otherwise not.
Now we only need to apply .input-focus class to the host element when input.focus is true, we can do this easily by @HostBinding:
@HostBinding('class.input-focus')
get isInputFocus() {
return this.input ? this.input.focus : false;
}
------
component:
import {Component, Input, ContentChild, AfterContentInit, HostBinding} from '@angular/core';
import {InputRefDirective} from 'app/lib/common/input-ref.directive';
@Component({
selector: 'au-fa-input',
templateUrl: './au-fa-input.component.html',
styleUrls: ['./au-fa-input.component.css']
})
export class AuFaInputComponent implements AfterContentInit {
@Input()
icon: string;
@ContentChild(InputRefDirective)
input: InputRefDirective;
@HostBinding('class.input-focus')
get isInputFocus() {
return this.input ? this.input.focus : false;
}
ngAfterContentInit() {
if (!this.input) {
console.error('You forgot pass in the input field');
}
}
get classes() {
const cssClasses = {};
if (this.icon) {
cssClasses['fa-' + this.icon] = true;
}
return cssClasses;
}
}
Directive:
import {Directive, HostListener} from '@angular/core';
@Directive({
selector: 'au-fa-input input'
})
export class InputRefDirective {
focus = false;
@HostListener('focus')
isFocus() {
this.focus = true;
}
@HostListener('blur')
isBlur() {
this.focus = false;
}
}
[Angular] @ContentChild with Directive ref的更多相关文章
- angular Creating a Directive that Adds Event Listeners
<span my-draggable>Drag ME</span> angular.module('dragModule', []) .directive('myDraggab ...
- Angular 下的 directive (part 2)
ngCloak ngCloak指令被使用在,阻止angular模板从浏览器加载的时候出现闪烁的时候.使用它可以避免闪烁问题的出现. 该指令可以应用于<body>元素,但首选使用多个ng ...
- angular 自定义指令 directive transclude 理解
项目中断断续续的用了下angular,也没狠下心 认真的学习.angular 特别是自定义指令这块 空白. transclude 定义是否将当前元素的内容转移到模板中.看解释有点抽象. 看解释有点抽象 ...
- Angular自定义指令directive:scope属性
在AngularJS中,除了内置指令如ng-click等,我们还可以自定义指令.自定义指令,是为了扩展DOM元素的功能.代码中,通过指定directive中的restrict属性,来决定这个指令是作为 ...
- Angular之指令Directive系列
项目筹备近期开启Angular学习,指令比较难理解所以记录备案,推荐Angualr实战学习视频大漠穷秋 Angular实战 一.指令directive概述 指令可以对元素绑定事件监听或者改变DOM结构 ...
- angular插件制作——Directive指令使用详解
1.replace——最简单的使用方法,直接将自定义标签替换为模板内的内容: html: <!DOCTYPE html> <html> <head> <me ...
- Angular 下的 directive (part 1)
directive 指令 Directive components 指令部分 使用指令自动引导一个AngularJS应用.ngApp指令指定应用程序的根元素,通常是放在页面的根元素如: < ...
- Angular ContentChild
contentchild // 使用方法 git clone https://git.oschina.net/mumu-osc/learn-component.git cd learn-compone ...
- angular之自定义 directive
1,指令的创建至少需要一个带有@Directive装饰器修饰的控制器类.@Directive装饰器指定了一个选择器名称,用于指出与此指令相关联的属性的名字. 2,创建一个highlight.direc ...
随机推荐
- arping---发送arp请求到一个相邻主机
arping命令是用于发送arp请求到一个相邻主机的工具,arping使用arp数据包,通过ping命令检查设备上的硬件地址.能够测试一个ip地址是否是在网络上已经被使用,并能够获取更多设备信息.功能 ...
- C# WinForm设置透明
1:通过设置窗体的 TransparencyKey实现 例:窗体中的白色会变成透明 this.BackColor =Color.White; this.TransparencyKey = ...
- STM32上使用JSON
一.STM32工程中添加JSON 最近在一网2串项目,串口和网口之间可能需要定义一下简单的通信协议,而通信协议上则需要去定义一下通信的数据格式,上次听剑锋说要用Json来定义,目前查了下资料具体如何去 ...
- Windows学习总结(4)——Host文件的作用和如何修改Host文件
本经验将为您介绍,什么是Host文件,Host文件作用,Host文件的位置等信息,以帮忙您了解Host文件. 方法/步骤 什么是HOST文件: Hosts是一个没有扩展名的系统文件,其基本作用就是将一 ...
- Myeclipse学习总结(2)——MyEclipse快捷键大全
1.ctrl+shift+R 打开资源 此组快捷键可以打开工程中任意一个文件,而本人只需按文件名或者mask名的字母顺序输入就会出现对应的文件或者在内容中某个关键字再按快捷键也可以的,例如:Custo ...
- 1.字符设备驱动------Linux中断处理体系结构
一.中断处理体系结构的初始化 Linux内核将所有的中断统一编号,使用一个irq_desc结构数组来描述这些中断;每个数组项对应一个中断,也可能是一组中断,它们共用相同的中断号,里面记录了中断的名称. ...
- Bag of Features (BOF)图像检索算法
1.首先.我们用surf算法生成图像库中每幅图的特征点及描写叙述符. 2.再用k-means算法对图像库中的特征点进行训练,生成类心. 3.生成每幅图像的BOF.详细方法为:推断图像的每一个特征点与哪 ...
- redhat6.5安装10201解决办法
rpm --import /etc/pki/rpm-gpg/RPM*yum install -y --skip-broken compat-libstdc++* elfutils-libelf* g ...
- 限制tomcat仅响应本机请求(转)
http://blog.bbzhh.com/index.php/archives/135.html 在VPS上搭建了nginx和tomcat应用,想通过nginx来反向代理127.0.0.1:8080 ...
- amazeui学习笔记--css(HTML元素5)--表格Table
amazeui学习笔记--css(HTML元素5)--表格Table 一.总结 1.基本样式:am-table:直接模块名 <table class="am-table"& ...