[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 ...
随机推荐
- css3.0+html绘制六边形
<section class="ass_desc"> <section class="ass_descLeft posRe"> < ...
- django模型系统二
常用查询及表关系的实现 1.常用查询 每一个django模型类,都有一个默认的管理器 objects QuerySet表示数据库中对象的列表,它可以有0到多个过滤器.过滤器通过给定参数,缩小查询范围. ...
- WPF框架ZFS
前文 项目开源地址(非正式版,开发版本), 码云Gitee地址: https://gitee.com/zhgg666/publicWpf XAML XAML能帮助团队真正实现UI与逻辑的剥离.XAM ...
- CSS布局篇——固宽、变宽、固宽+变宽
学了前端挺久了.近期写一个项目測试系统,布局时发现自己对变宽+固宽的布局还没有全然掌握,所以在这里总结一下,以后须要的时候回头看看. 1.最简单的当然是一列或多列固宽 比如两列固宽: <1> ...
- 很好的资源 for android
//texttospeach http://examples.javacodegeeks.com/android/core/text-to-speech/android-text-to-speech- ...
- Linux shell command学习笔记(一)
Shell的种类有很多种,例如CSH,Bourne Shell,Korn Shell.在现在的大多数Linux发行版中,默认的Shell一般都是Bourne again shell(bash). &l ...
- vue2.0实现银行卡类型种类的选择
功能效果:vue2.0实现银行卡类型种类的选择 图片.png 参考代码如下: <template> <div class="app"> <header ...
- C# Unity依赖注入利用Attribute实现AOP功能
使用场景? 很多时候, 我们定义一个功能, 当我们要对这个功能进行扩展的时候, 按照常规的思路, 我们一般都是利用OOP的思想, 在原有的功能上进行扩展. 那么有没有一种东西, 可以实现当我们需要扩展 ...
- 洛谷 P1781 宇宙总统
P1781 宇宙总统 题目背景 宇宙总统竞选 题目描述 地球历公元6036年,全宇宙准备竞选一个最贤能的人当总统,共有n个非凡拔尖的人竞选总统,现在票数已经统计完毕,请你算出谁能够当上总统. 输入输出 ...
- [Python] Different ways to test multiple flags at once in Python
x, y, z = 0, 1, 0 if x == 1 or y == 1 or z == 1: print('passed') if 1 in (x, y, z): print('passed') ...