[Angular] Export directive functionalities by using 'exportAs'
Directive ables to change component behaives and lookings. Directive can also export some APIs which enable behaivor changes control by outside directive iteslf.
For example, we have an tooltip:

It is a directive:
import { Input, Directive, ElementRef, OnInit } from '@angular/core';
@Directive({
selector: '[tooltip]',
exportAs: 'tooltip'
})
export class TooltipDirective implements OnInit {
tooltipElement = document.createElement('div');
@Input()
set tooltip(value) {
this.tooltipElement.innerText = value;
}
hide() {
this.tooltipElement.classList.remove('tooltip--active');
}
show() {
this.tooltipElement.classList.add('tooltip--active');
}
constructor(
private element: ElementRef
) {}
ngOnInit() {
this.tooltipElement.className = 'tooltip';
this.element.nativeElement.appendChild(this.tooltipElement);
this.element.nativeElement.classList.add('tooltip-container');
}
}
This tooltip should be hidden by default.
We want to toggle show/hide by mouse over the '(?)' span:

So just export the directive:
@Directive({
selector: '[tooltip]',
exportAs: 'tooltip'
})
The html:
<div>
<label>
Credit Card Number
<input
name="credit-card"
type="text"
placeholder="Enter your 16-digit card number"
credit-card>
</label>
<label
tooltip="3 digial only"
#myTooltip="tooltip"
>
Enter your security code
<span
(mouseover)="myTooltip.show()"
(mouseout)="myTooltip.hide()">
(?)
</span>
<input type="text">
</label>
</div>
And html get use template ref to get the directive:
#myTooltip="tooltip"
Then we can control it in other place:
<span
(mouseover)="myTooltip.show()"
(mouseout)="myTooltip.hide()">
(?)
</span>
[Angular] Export directive functionalities by using 'exportAs'的更多相关文章
- [Angular 2] Directive intro and exportAs
First, What is directive, what is the difference between component and directive. For my understandi ...
- [Angular] Custom directive Form validator
Create a directive to check no special characters allowed: import {Directive, forwardRef} from '@ang ...
- [Angular] Test Directive
directive: import { Directive, HostListener, HostBinding, ElementRef } from '@angular/core'; @Direct ...
- [Angular] Using directive to create a simple Credit card validator
We will use 'HostListener' and 'HostBinding' to accomplish the task. The HTML: <label> Credit ...
- 关于angular 自定义directive
关于angular 自定义directive的小结 首先我们创建一个名为"expander"的自定义directive指令: angular.module("myApp& ...
- angular service/directive
<html class=" js cssanimations csstransitions" ng-app="phonecatApp" > < ...
- 一个Demo就懂的Angular之directive
<body> <div ng-controller="myCtrl"> <hello-word></hello-word> < ...
- angular 中 directive中的多个指令
<div ng-controller="ctrl1"> <superman weight length speed>superman</superma ...
- Angular中directive——scope选项与绑定策略,这个也经常迷惑的。
开门见山地说,scope:{}使指令与外界隔离开来,使其模板(template)处于non-inheriting(无继承)的状态,当然除非你在其中使用了transclude嵌入,这点之后的笔记会再详细 ...
随机推荐
- 洛谷 P2692 覆盖
P2692 覆盖 题目背景 WSR的学校有B个男生和G个女生都来到一个巨大的操场上扫地. 题目描述 操场可以看成是N 行M 列的方格矩阵,如下图(1)是一个4 行5 列的方格矩阵.每个男生负责打扫一些 ...
- android插件式开发资料整理
1.DL : Apk动态载入框架 2.android中的动态载入机制
- JavaBean对象转map
可能会经常使用的方法,利用反射将javaBean转换为map.稍作改动就可以转为想要的其它对象. /** * obj转map * @param map 转出的map * @param obj 须要转换 ...
- windows下使用cpanm进行模块安装
windows下使用cpanm进行模块安装 要放假了,突然想整理一下手头上的软件,突然发现perl的安装模块这个功能不能用. 弄了一下,使得windows 下 perl 的 cpanm能用,避免成天为 ...
- GestureDetector-onfling不执行
今天在做计算器的时候,遇到了一个问题,就是当我使用GestureDetector的时候,onFling方法不执行,而其他的可以执行.代码如下 @Override public boolean onDo ...
- dot-files/directories 点开头的文件或文件夹(windows/linux)
What's so special about directories whose names begin with a dot? 不管是 windows 系统,还是类 linux 系统,以点开头的文 ...
- 4、python基本知识点及字符串常用方法
查看变量内存地址 id(变量名) ni = 123 n2 = 123 ni和n2肯定是用的两份内存,但是python对于数字在-5~257之间的数字共用一份地址,范围可以修改 name = ‘李璐 ...
- Oracle批量插入在C#中的应用
public void SetUserReportResult(int[] reportId, bool isReceive, string result) { if (reportId == nul ...
- 【重拾Effective Java】一
之前看这本<Effective Java(第二版)>都是非常早曾经了.这本书确实是本好书.须要细嚼慢咽,每次看都有不同的体验. 在此写博客巩固一下. 第一章.创建和销毁对象 考虑用静态工厂 ...
- Dll的链接使用细节
关于Dll Dll.Exe 都是PE格式的二进制文件. Dll相当于Linux操作系统下的so文件 1 基地址(Base Address)和相对地址(RelativeVirtual Address) ...