在Angular 2中的运行时添加指令

在Angular 2中的运行时添加指令,angular,runtime,directive,Angular,Runtime,Directive,当存在类时,我想通过指令向元素添加一些行为。 在我当前的测试中,选择器是类:“next”。指令正在处理在应用程序引导程序上具有类的h5,但我在运行时应用于类的h5不是指令正在处理的。为什么? 我创建了一个plunkr来说明我的问题: 从'@angular/core'导入{指令,HostBinding}; @指示({ 选择器:'.next' }) 导出类NextDirective{ @HostBinding()innerText=`我是一个指令` 构造函数(){ console.log('Nex

当存在类时,我想通过指令向元素添加一些行为。
在我当前的测试中,选择器是类:“next”。指令正在处理在应用程序引导程序上具有类的
h5
,但我在运行时应用于类的
h5
不是指令正在处理的。为什么?

我创建了一个plunkr来说明我的问题:

从'@angular/core'导入{指令,HostBinding};
@指示({
选择器:'.next'
})
导出类NextDirective{
@HostBinding()innerText=`我是一个指令`
构造函数(){
console.log('NextComponent');
}
}
福
酒吧
Toggle有下一个类:{hasNext}

很遗憾,这不受支持,
import {Directive, HostBinding} from '@angular/core';
@Directive({
  selector: '.next'
})
export class NextDirective{
  @HostBinding() innerText = `I'm a directive!`
  constructor() {
    console.log('NextComponent');
  }
}

    <div>
      <h5 [ngClass]="{next:hasNext}">FOO</h5>
      <h5 class="next">BAR</h5>
      <button (click)="hasNext = !hasNext">Toggle</button> has next class: {{hasNext}}
    </div>