How can I call function from directive after component's rendering?
I have component:
export class Component {
ngAfterContentInit() {
// How can i call functionFromDirective()?
}
}
And I want call this function:
export class Directive {
functionFromDirective() {
//something hapenns
}
How can i do this?
Calling the method from within a component is not a good idea. Using a directive helps in a modular design, but when you call the method, you get a dependency from the component to the directive.
Instead, the directive should implement the AfterViewInit interface:
This way, your component doesn't have to know anything about the directive.
You can retrieve Directive from Component's template with
ViewChild
like this:In your component: