20

I am trying to call a public function which is inside my directive from component by getting hold of the directive via viewchild like this

 @ViewChild('myDirective') myDirective; .... myDirective.nativeElement.myFunction(); 

But I get error that the myFunction does not exist. Is there a way to call a function which is iniside a directive?

2

3 Answers 3

28

DEMO - How to call Directive's function from Component - check browser's console


1) I hope you are importing myDirective directive.

import {myDirective} from './Directive'; 

2)

@ViewChild(myDirective) vc:myDirective; ///<<<@@@removed '' from ('myDirective') 

3)

ngAfterViewInit(){ this.vc.myFunction(); ///<<@@@ no need to use nativeElement } 

4) Or some button's click event

click(){ this.vc.myFunction(); } 
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, this worked with ViewChild and by setting the directive instead of the #myDirective I was using with the ''. But it makes wonder how to accomplish this when using multiple directives. In this case I am only using one directive.
With multiple directives mean how and what???. Ask question and get answer. tell us your scenario and we'll try to resolve it.
Thanks, what I mean is if you have e.g. from your plunkr <h2 someDirective> Angular2</h2> <h3 someDirective> Test 3</h3> How would you reference the h3 directive
just what i was looking for, awesome find!
This seems deprecated. Cannot read property 'callme' of undefined. please see stackblitz.com/edit/angular-playground-1di2vk?file=app/…
|
9

Use @ContentChild(). A directive doesn't have a view.

Call this.myDirective.nativeElement.myFunction() in ngAfterContentChecked() to ensure that this.myDirective... is already initialized.

5 Comments

Thanks for your reply. I changed ViewChild to ContentChild but now myDirective is undefined. Any idea why this could be happening?
Did you find out why?
A directive doesn't have a view, it only can have content - the content of it's host element.
with ngAfterContentChecked, check for not null and not undefined using "if (this.vc) {this.vc.myFunction()}" - checked for ng5 & ng6
that should be accept answer. Your solution help me so much. thank alot.
1

You can use the "exportAs" property of the directive to then get the directive's instance from the decorated component.

For instance:

Create a dummy directive:

import { Directive } from '@angular/core'; @Directive({ standalone: true, selector: '[appDemoDirective]', exportAs: 'aDirective' // add here a name to get the instance later }) export class DemoDirective { aFunction(aParameter: string) { console.log(aParameter); } } 

You can then get the directive instance using #variableName='aDirective'; i.e. a variable name = the export name you gave above

const template = ` <button appDemoDirective #directiveRef='aDirective' (click)="directiveRef.aFunction(message)">Click me</button> ` @Component({ selector: 'app-root', template: template }) export class AppComponent { public message = "Hello"; } 

Clicking on button should call the directive's function and print "Hello" in this case

1 Comment

Exactly what I was looking for! Great answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.