1

I have created a directive for a textarea (TextAreaExt) and i want to call a method of that directive from a component it is used in. The selector of the directive is "textarea" (no attribute, affects all textarea elements). In my component if i do the following:

@ViewChild('TextAreaExt') txt: TextAreaExt;

I can call methods on txt successfully. But what if i had multiple textareas? I would like to do it like this instead:

@ViewChild('txt') txt: TextAreaExt;

Unfortunately, the above line does not return a TextAreaExt class, but instead returns an ElementRef.

How could i solve this problem so that i could have multiple text areas?

1 Answer 1

0

This should do what you want:

@ViewChild('txt', {read: TextAreaExt}) txt: TextAreaExt;

If there are more directives, components or other DI tokens registered for an element, read allows you to specify, which one you want to get returned.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Worked like a charm

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.