angular - How to listen to children elements events in a directive? -
since there no template, what's best way listen child elements event in directive? possible hostlistener
? if not, there other way?
there's similar question: how listen child event parent directive in angular2, suggested approach not solve problem, directive , child element aren't in same template (directive on host).
cheers!
edit #1
this how i'm doing (there has better way):
first injecting elementref
directive:
constructor(private elview: elementref) {}
then binding jquery (or plain js):
$(this.elview.nativeelement) .on('drag', '#childid', this.slide.bind(this))
if event want listen native dom event bubbles, can use @hostlistener()
@hostlistener('click', ['$event']) handleclick(event) { // handle event }
if outputs of child components, can query them , subscribe outputs
@contentchildren(childcomponent) children:querylist<childcomponent>; ngaftercontentinit() { this.children.toarray().foreach((item) => { item.someoutput.subscribe(...); }); }
or can use approach used in answer linked in question.
Comments
Post a Comment