I have two components: ComponentOne
and ComponentTwo
.
@NgComponent(...)
class ComponentOne {
}
<div>
<h1>Component One</h2>
<content></content>
<div>
@NgComponent(...)
class ComponentTwo {
}
<div>
<h1>Component Two</h2>
<div>
Then I have the following markup:
<component-one>
<component-two></component-two>
</component-one>
How do I reference ComponentTwo
from ComponentOne
. To be more specific, I have a method which handles click event and needs to delegate that click event to it's child. Which is the best way to do that?
The answer to your question resides in this tutorial page : https://angular.io/docs/dart/latest/tutorial/toh-pt3.html
Actually you can use the
@Input parent
directive in your component to link it to its parent in an abstract manner with a "target attribute property" [parent]=meYou can therefore declare an interface you expect the parent to implement to have a unidirectional link
I would inject
ComponentOne
toComponentTwo
and in the constructor ofComponentTwo
assignComponentTwo
toComponentOne
.You could also create a Directive that does only that, to avoid tight coupling of ComponentTwo to ComponentOne.
Maybe there is a better way but I didn't see one yet.
You have to set
visibility
ofComponentOne
to children.See also angular 2 / typescript : get hold of an element in the template