I'm attempting to invoke a method from a directive. Let's assume I have a directive myDirective.ts
@Directive({
selector: '[something]',
})
export class myDirective implements OnInit {
....
public myMethod(){
console.log("it works")
}
....
}
and component.ts (this is what I found but on this.directive I get an error saying object is possibly null)
@Component({
selector: '...',
templateUrl: '....html',
styleUrls: ['....css'],
})
export class MyComponent implements OnInit {
@ViewChild(myDirective) directive = null
ngOnInit() {
}
onClickFunction() {
return (
this.directive.myMethod();
);
}
}
How can I effectively call this method in the component?