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

  1. angular Creating a Directive that Adds Event Listeners

    <span my-draggable>Drag ME</span> angular.module('dragModule', []) .directive('myDraggab ...

  2. Angular 下的 directive (part 2)

    ngCloak ngCloak指令被使用在,阻止angular模板从浏览器加载的时候出现闪烁的时候.使用它可以避免闪烁问题的出现.   该指令可以应用于<body>元素,但首选使用多个ng ...

  3. angular 自定义指令 directive transclude 理解

    项目中断断续续的用了下angular,也没狠下心 认真的学习.angular 特别是自定义指令这块 空白. transclude 定义是否将当前元素的内容转移到模板中.看解释有点抽象. 看解释有点抽象 ...

  4. Angular自定义指令directive:scope属性

    在AngularJS中,除了内置指令如ng-click等,我们还可以自定义指令.自定义指令,是为了扩展DOM元素的功能.代码中,通过指定directive中的restrict属性,来决定这个指令是作为 ...

  5. Angular之指令Directive系列

    项目筹备近期开启Angular学习,指令比较难理解所以记录备案,推荐Angualr实战学习视频大漠穷秋 Angular实战 一.指令directive概述 指令可以对元素绑定事件监听或者改变DOM结构 ...

  6. angular插件制作——Directive指令使用详解

    1.replace——最简单的使用方法,直接将自定义标签替换为模板内的内容:  html: <!DOCTYPE html> <html> <head> <me ...

  7. Angular 下的 directive (part 1)

    directive  指令 Directive components  指令部分   使用指令自动引导一个AngularJS应用.ngApp指令指定应用程序的根元素,通常是放在页面的根元素如: < ...

  8. Angular ContentChild

    contentchild // 使用方法 git clone https://git.oschina.net/mumu-osc/learn-component.git cd learn-compone ...

  9. angular之自定义 directive

    1,指令的创建至少需要一个带有@Directive装饰器修饰的控制器类.@Directive装饰器指定了一个选择器名称,用于指出与此指令相关联的属性的名字. 2,创建一个highlight.direc ...

随机推荐

  1. apache wicket 7.X之HelloWorld

    Wicket是什么 Wicket一个开发Java Web应用程序框架. 它使得开发web应用程序变得easy而轻松. Wicket利用一个POJO data beans组件使得它能够与不论什么持久层技 ...

  2. libssh2进行远程运行LINUX命令

    /** * CSSHClient.h * @file 说明信息.. * DATE February 13 2015 * * @author Ming_zhang */ #ifndef _CSSHCLI ...

  3. tooltip两个特殊的属性

    <body style="margin: 50px;"> <!--两个特殊的属性--> <div id="selection"&g ...

  4. 升级你的Linux日志系统

    650) this.width=650;" onclick='window.open("http://blog.51cto.com/viewpic.php?refimg=" ...

  5. JavaScript--数据结构与算法之排序

    排序总结————常见的排序 常见的9中排序(冒泡,选择,插入(二分插入,希尔),归并,快速,堆,计数,基数,桶排序)可分为两类 比较排序:冒泡,选择,插入(二分插入,希尔),归并,堆,快速 非比较排序 ...

  6. 关于Django的登录系统

    首先需要明确的是登录的本质:登录就是服务器确认当前用户的身份,并将数据库中的记录提取匹配 默认的登录系统是用户名密码方式,这种方式很平常,也没什么特别的.这里主要说的是第三方验证登录 通常第三方验证登 ...

  7. install-软件安装跟push的区别

    今天在做项目的时候,需要往一个user版本的手机中安装一个应用,就在网上查了相应的方法,可以使用如下命令 adb install -r out/target/product/vanzo6752_lwt ...

  8. HTML基础第十一讲---背景标志

    转自:https://i.cnblogs.com/posts?categoryid=1121494 您是否老觉得网页「空空的」,没错!一个可能是我们还没有很多内容,另一个可能则是我们还没有设定网页背景 ...

  9. css的三种表现形式

    1.行内样式(内嵌样式):结构的内部,即写在标签内的样式:写在标签的开始部分内部,style属性当中:<标记 style="样式的属性名1:样式的属性值1:属性名2:属性值2:.... ...

  10. 如何不使用js实现鼠标hover弹出菜单效果

    最近看到很多同学在实现鼠标hover弹出菜单的效果时都是用的js代码去实现的,默认给弹出隐藏掉,通过js事件绑定动态的显/隐弹出菜单元素. <ul> <li>主页</li ...