简体   繁体   中英

Accessing angular2 directive from component

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM