0

I'm looking for solution where I can switch dynamically (depending on the component we are in) value in parent component. Value should be different in each component.

Is the service not too much for this kind of solution? Is there a simpler solution in Angular?

Parent - Some text {{ title }}

Child1 - title = "title one" Child2 - title = "title two"

etc.

2 Answers 2

2

Try using EventEmitter:

child-component.ts

import { Output, EventEmitter } from '@angular/core'; title: string; @Output() titleChange: EventEmitter<any> = new EventEmitter(); ngOnInit() { this.title = 'child title'; this.titleChange.emit(this.title); } 

parent-component.html

<p>{{title}}</p> <app-child (titleChange)="onTitleChange($event)"></app-child> 

parent-component.ts

title: string onTitleChange(value: string) { this.title = value; } 
Sign up to request clarification or add additional context in comments.

1 Comment

I used that solution. But Angular where throwing error every time it switch "panel" so I have to add onTitleChange(value: string) { setTimeout(()=>{ this.title = value; });
0

Are you using the RoutingModule? If yes, you could add a data attribute (i.e. title) in your routing definition and then subscribe to route changes in the parent component.

For example like this: https://stackoverflow.com/a/58863966/3073719

1 Comment

Unfortunately I'm not using RoutingModule here. It's more complicated :P

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.