[英]How to pass a value into the Directive?
I have a Directive: 我有一个指令:
@Directive({
selector: 'random'
})
export class RandomDirective {
@Input text: string;
}
Can I pass a value text
into the Directive? 我可以将值
text
传递到指令中吗?
<div random></div>
Thanks 谢谢
Try this: 尝试这个:
<div random [text]="'sometext'"></div>
Directive: 指示:
@Directive({
selector: '[random]',
host: {
'(mouseenter)': 'onMouseEnter()', //just for test
}
})
export class RandomDirective {
@Input() text: string;
//method for test
onMouseEnter() {
console.log(this.text);
}
}
Plunker - check console. 柱塞 -检查控制台。
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";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.