Les directives
import { Directive } from '@angular/core'
@Directive({
selector: '[highlight]'
})
export class HighlightDirective {
// Code here ;)
}
import { Directive, ElementRef } from '@angular/core'
@Directive({
selector: '[highlight]'
})
export class HighlightDirective {
constructor(private _elementRef: ElementRef) {
this._elementRef.nativeElement.style.backgroundColor = 'red'
}
}
@Component({
selector: 'child-cmp',
template: '<p>child</p>'
})
class ChildCmp {
doSomething() {}
}
@Component({
selector: 'some-cmp',
template: '<child-cmp></child-cmp>',
directives: [ChildCmp]
})
class SomeCmp {
@ViewChild(ChildCmp) child:ChildCmp;
ngAfterViewInit() {
// child is set
this.child.doSomething();
}
}
@Component({
selector: 'child-cmp',
template: '<p>child</p>'
})
class ChildCmp {
doSomething() {}
}
@Component({
selector: 'some-cmp',
template: '<child-cmp #child></child-cmp>',
directives: [ChildCmp]
})
class SomeCmp {
@ViewChild('child') child:ChildCmp;
ngAfterViewInit() {
// child is set
this.child.doSomething();
}
}
@HostListener('mouseenter') onMouseEnter() {
this.highlight('yellow');
}
@HostListener('mouseleave') onMouseLeave() {
this.highlight(null);
}
private highlight(color: string) {
this._elementRef.nativeElement.style.backgroundColor = color;
}