[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 ...
随机推荐
- [lougu1341]无序字母对
Description: 给定n个各不相同的无序字母对(区分大小写,无序即字母对中的两个字母可以位置颠倒).请构造一个有n+1个字母的字符串使得每个字母对都在这个字符串中出现. Solution: 欧 ...
- Jenkins学习总结(2)——Jenkins+Maven进行Java项目持续集成
最近配置了Jenkins服务器,记录下基本过程.(当然还遇到了若干小问题,兵来将挡水来土掩就是了) Jenkins安装 安装Tomcat 从Jenkins官网下载jenkins.war文件.官网地址: ...
- 平衡数之Treap
#include <memory>//智能指针头文件 #include <random>//随机数头文件 #include <iostream> #include ...
- 运行 CMD 时,參数加引號常见问题
在调用 CMD 时.如脚本中用 WScript.Shell 调用. 假设參数中有包括空格的长路径名时,必需要加引號才干正确被识别. 是的,大家都知道要加引號.但怎么加却easy被误解.这个问题,不时地 ...
- Android线程池(二)——ThreadPoolExecutor及其拒绝策略RejectedExecutionHandler使用演示样例
MainActivity例如以下: package cc.vv; import java.util.concurrent.LinkedBlockingQueue; import java.util.c ...
- 28.lambda表达式与多线程
#include <iostream> #include <thread> #include <Windows.h> #include <chrono> ...
- BZOJ2733: [HNOI2012]永无乡(线段树合并)
Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...
- CF1009F Dominant Indices(树上DSU/长链剖分)
题目大意: 就是给你一棵以1为根的树,询问每一个节点的子树内节点数最多的深度(相对于这个子树根而言)若有多解,输出最小的. 解题思路: 这道题用树链剖分,两种思路: 1.树上DSU 首先想一下最暴力的 ...
- 你真的懂Flask中浅谈蓝图Blueprint吗?
一,什么是Flask中的蓝图Blueprint Blueprint是用于实现Flask框架中单个应用的视图,模板,静态文件的集合. Blueprint 是一个存储操作(路由映射)方法的容器,这些操作在 ...
- Maven搭建hadoop环境报Missing artifact jdk.tools:jdk.tools:jar:1.7(5种办法,2种正解)
刚刚写的那一篇,是网上比较主流的解决办法. 鉴于实际情况,有伙伴的机器上没有遇到这个问题,我们再探究原因,最终还有4种情况需要说明. 先说,另外一种"正解". <depend ...