I am building an attribute directive, which changes the background color of the host element accounting to a "quality" @input.
I found that if I implement ngOnChanges as a lambda expression, the ngOnchanges method would not be invoked when input changes.
My playground:
https://stackblitz.com/edit/angular-6-playground-lqwps2?file=src%2Fapp%2FmyOrder.directive.ts
@Directive({
selector: '[my-order]'
})
export class MyOrderDirective {
@Input()
quality: number = 1;
@HostBinding('style.background-color')
backgroundColor: string;
// ************* works ********************
// ngOnChanges(changes: SimpleChanges) {
// if (this.quality % 2 == 0) {
// this.backgroundColor = 'red';
//
// } else {
// this.backgroundColor = 'blue';
// }
//
// };
// ******* lambda expression does NOT work ***********
ngOnChanges = (changes: SimpleChanges) => {
if (this.quality % 2 == 0) {
this.backgroundColor = 'red';
} else {
this.backgroundColor = 'blue';
}
};
// ******************** Not work *********************
// ngOnChanges = function (changes: SimpleChanges) {
// if (this.quality % 2 == 0) {
// this.backgroundColor = 'red';
//
// } else {
// this.backgroundColor = 'blue';
// }
//
// };
constructor() {
}
}