Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 基于动态添加css类的Angular2指令_Javascript_Typescript_Angular_Angular2 Directives - Fatal编程技术网

Javascript 基于动态添加css类的Angular2指令

Javascript 基于动态添加css类的Angular2指令,javascript,typescript,angular,angular2-directives,Javascript,Typescript,Angular,Angular2 Directives,角度路由器的[routerLink]指令将CSS类路由器链接活动添加到活动链接。我想基于这个类编写指令 import { Directive, ElementRef} from '@angular/core'; @Directive({selector: '.router-link-active'}) export class HighlightDirective { constructor(el:ElementRef) { el.nativeElement.style.

角度路由器的
[routerLink]
指令将CSS类
路由器链接活动
添加到活动链接。我想基于这个类编写指令

import { Directive, ElementRef} from '@angular/core';
@Directive({selector: '.router-link-active'})
export class HighlightDirective {
    constructor(el:ElementRef) {
        el.nativeElement.style.backgroundColor = 'yellow';
    }
}
然而,它并没有像我预期的那样工作

若指令在模板中硬编码,则该指令有效:

<a class="router-link-active">Feature1</a>
虽然添加了CSS类,但不会触发指令代码


这是我学习Angular2的第一步,也许解决方法很简单,但我找不到答案

这是不受支持的。Angular2仅对与静态添加到组件模板的HTML匹配的选择器实例化组件和指令。任何动态内容都将被忽略(使用
[innerHTML]=“…”
或动态添加的属性或类添加的HTML)

<a [routerLink]="['/feature1']">Feature1</a>