Janier
Janier

Reputation: 4412

accessing other components which are not children in Angular

How can I access other components that are in the same view but aren't children? My parent component has some components.

parent.component.html

<html>
  <address-component></address-child>
  <name-component></name-component>
</html>

So how can I access name-component from the address-component? They do not have a relationship of their own except that they lie in the same view.

Upvotes: 0

Views: 309

Answers (1)

Uğur Din&#231;
Uğur Din&#231;

Reputation: 2455

If you don't want to use a service, you can try this child1-parent-child2 communication:

<address-component (updateNameComponent)="nameComponent.yourMethodName()"></address-child>
<name-component #nameComponent ></name-component>

And in your address component, you would have this:

@Output() updateNameComponent = new EventEmitter();

and you can trigger nameComponent.yourMethodName from address component using this:

updateNameComponent.emit();

Upvotes: 1

Related Questions