Typescript 如何将值传递到指令中?

Typescript 如何将值传递到指令中?,typescript,angular,Typescript,Angular,我有一个指示: @Directive({ selector: 'random' }) export class RandomDirective { @Input text: string; } @Directive({ selector: '[random]', host: { '(mouseenter)': 'onMouseEnter()', //just for test } }) export class RandomDirective { @Input

我有一个指示:

@Directive({
  selector: 'random'
})
export class RandomDirective {
  @Input text: string;
}
@Directive({
  selector: '[random]',
  host: {
    '(mouseenter)': 'onMouseEnter()', //just for test
  }
})
export class RandomDirective {

  @Input() text: string;

  //method for test
  onMouseEnter() {
   console.log(this.text);
  }
}
我可以将值
文本
传递到指令中吗

<div random></div>

谢谢

试试这个:

<div random [text]="'sometext'"></div>
-检查控制台

import {Component, Directive, Input} from 'angular2/core'

@Directive({
    selector: '[random]'
})
export class RandomDirective implements OnChanges {

  @Input() text:string;

   ngOnChanges(...args: any[]) {
        console.log('onChange fired');
        console.log(args[0].text)
    }
}



@Component({
  selector: 'my-app', 
  providers: [],
  template: `

    <p random  [text]="value"></p>
  `,
  directives: [RandomDirective]
})

export class App {
  value:string="micronyks";  
}
从'angular2/core'导入{组件、指令、输入}
@指示({
选择器:“[随机]”
})
导出类指令实现OnChanges{
@Input()文本:字符串;
ngOnChanges(…参数:任意[]){
log('onChange-fired');
console.log(args[0].text)
}
}
@组成部分({
选择器:“我的应用程序”,
提供者:[],
模板:`

`, 指令:[随机指令] }) 导出类应用程序{ 值:string=“micronyks”; }