I am trying to pass a value to the directive's custom attribute, but when I try to log it it says it is undefined.
This is my component.html:
<div appRadialMenu [step]="4"></div>
This is my directive.ts:
import { Directive, ElementRef, Input, OnInit } from '@angular/core';
// jQuery
import * as $ from 'jquery';
@Directive({
selector: '[appRadialMenu]'
})
export class RadialMenuDirective implements OnInit {
@Input() step;
constructor(private elRef: ElementRef) {
$(elRef.nativeElement).click(function () {
console.log(this.step);
}
}
ngOnInit() {
console.log(this.step);
}
}
Edit: I also tried using
@Input('step') step;
Edit 2: Thank you guys, it worked and I made the following changes:
I changed the click event binding and I moved it to the ngOnInit, also I changed the selector
selector: 'appRadialMenu'
// ......
@Input('step') step;
constructor(private elRef: ElementRef) {}
ngOnInit() {
$(this.elRef.nativeElement).click(() => {
console.log(this.step);
});
}
component.html:
<appRadialMenu [step]="4"></appRadialMenu>