For example we have a component:

<Card ></Card>

And a driective:

<Card highlighted></Card>

We want to get the driective instant inside the component code, we can use @ViewChild:

@ViewChild(CardComponennt, {read: HighlightedDirective}) highlighted: HighlightedDirective

Then we can access the instant in ngAfterViewInited lifecycle hook.

-----

Another way to access driective is inside component template. we need to use 'exportAs' from  the directive:

@Directive({
name: 'highlighted',
exportAs: 'hl'
}) ... toggle () {...}

Inside the template, we can get it from the template reference:

<Card highlighted #hlref="hl"></Card>

<button (click)="hlref.toggle()"></button>

[Angular] @ViewChild read custom directive and exportAs的更多相关文章

  1. Angular自定义指令(directive)

    angular自定义指令,意我们可以通过angula自己定义指令,来实现我们的特殊要求,为所欲为,一支穿云箭,千军万马来相见 多少年的老规矩了,先看代码: <!DOCTYPE html> ...

  2. angular的GitHub Repository Directive Example学习

    angular的GitHub Repository Directive Example学习 <!DOCTYPE html> <html ng-app="myApp" ...

  3. angular自定义指令-directive

    Directive究竟是个怎么样的一个东西呢?我个人的理解是这样的:将一段html.js封装在一起,形成一个可复用的独立个体,具体特定的功能.下面我们来详细解读一下Directive的一般性用法. v ...

  4. [Angular] Custom directive Form validator

    Create a directive to check no special characters allowed: import {Directive, forwardRef} from '@ang ...

  5. Angular之 Scope和 Directive

    ---------------------------Scope-------------------------------- https://docs.angularjs.org/guide/sc ...

  6. [Angular] Create a custom validator for reactive forms in Angular

    Also check: directive for form validation User input validation is a core part of creating proper HT ...

  7. Angular 学习笔记 (Custom Accessor + Mat FormField + Custom select)

    custom form control 之前就写过了,这里简单写一下. 创建一个组件实现 ControlValueAccessor 接口 @Component({ providers: [ { pro ...

  8. -_-#【Angular】自定义指令directive

    AngularJS学习笔记 <!DOCTYPE html> <html ng-app="Demo"> <head> <meta chars ...

  9. angular ViewChild ContentChild 系列的查询参数

    官方说明 官方文档 在调用 NgAfterViewInit 回调函数之前就会设置这些视图查询. 元数据属性: selector - 用于查询的指令类型或名字. read - 从查询到的元素中读取另一个 ...

随机推荐

  1. 使用rssh创建一个安全的文件服务器

    使用rssh创建一个安全的文件服务器 目前有这样一个需求,公司需要一台linux服务器作为文件服务器,但是基于安全性考虑,我不想使用ftp或者samba,但又必须允许用户上传文件.怎么办呢? 因为是l ...

  2. HDU1556---树状数组 | 线段树 |*

    输入n,紧接n行,每行a,b n个气球,a,b表示从第a到第b个气球涂一次色,输出每个球最终的涂几次色 暴力超时,优化数据结构 1.树状数组 #include<iostream> #inc ...

  3. Java中的抽象和封装

    一.面向对象和面向过程的区别 面向对象: 核心:封装了属性和方法的类,以数据为中心,实现了类级别的代码重用 面向对象因为采用了类,所以具有继承和多态特性,可以进一步重用代码和简化编程 面向过程: 核心 ...

  4. 多目录,多可执行文件的Makfile的编写

    1.前言 在目前的工作中,我遇到这样的一个工作情景,可以认为我要开发一个库,这个库的有多个模块,每个模块的.c放到src中,.h放到include中,这应该是个标准做法. drwxr-xr-x. ro ...

  5. VS2013 更改MFC标题栏图标和生成的执行文件图标

    创建一个新工程,可以什么都不加.打开.rc,  创建或打开Icon资源(以下都以Icon为例).     单击工程窗口的资源视图标签,选中资源ID为IDR_MAINFRAME图标资源,然后按Delet ...

  6. C++内联汇编,输出人物名字

    DWORD PeopleBase =0x9CD674;//天龙人物基址 //获取人物名称 char * CData::GetPeopleName() {     char * name="& ...

  7. 使用Laravel将数据Excel导出的方法

    1.copmposer下载maatwebsite/excel 2.在控制器引入:use Excel; 3.将要导出的数据处理成数组,第一组数据为表的字段名,如图 4.导出成表格 Excel::crea ...

  8. Guava源码学习(三)ImmutableCollection

    基于版本:Guava 22.0 Wiki:Immutable collections 0. ImmutableCollection简介 类似于JDK的Collections.unmodifiableX ...

  9. 蓝桥杯模拟赛 引爆炸弹-并查集+DFS

    引爆炸弹 在一个 n×m的方格地图上,某些方格上放置着炸弹.手动引爆一个炸弹以后,炸弹会把炸弹所在的行和列上的所有炸弹引爆,被引爆的炸弹又能引爆其他炸弹,这样连锁下去. 现在为了引爆地图上的所有炸弹, ...

  10. (10)C#静态方法,静态字段,静态类,匿名类

    6.静态方法 使用静态方法就可不必用类的实例化调用次函数 class Test { public static void method() { ........ } //当调用一个method()时就 ...