У меня есть директива:
@Directive({
selector: 'random'
})
export class RandomDirective {
@Input text: string;
}
Могу ли я передать text
значения в Директиву?
<div random></div>
благодаря
Попробуй это:
<div random [text]="'sometext'"></div>
Директива:
@Directive({
selector: '[random]',
host: {
'(mouseenter)': 'onMouseEnter()', //just for test
}
})
export class RandomDirective {
@Input() text: string;
//method for test
onMouseEnter() {
console.log(this.text);
}
}
Plunker - контрольная консоль.
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";
}