Change a property of a component from another component and render it in the html in angular 2

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Change a property of a component from another component and render it in the html in angular 2



I have 3 components 'starter-left-side', 'starter-content', 'camera'. 1 service 'generalParameters'. In the generalParameters, I have 2 properties; 'contentHeader' & contentDescription which has default string values respectively.



Upon initialization of the starter-content, I get these values from the generalParameters and render it in starter-content.html. When I want to go to the camera.component, I just click on the link to the camera.component via the starter-left-side also, I have a method in the starter-left-side that sets the property value of the generalProperties as soon as the link is clicked so it can be used by the starter-content again.



I can successfully change the values in the generalProperties but the problem is, it is not rendered in the starter-component anymore. I do not know on which time of the life cycle hooks should I get the values from the generalProperties again so it can be rendered in the starter-content.html.



generaParameters.service.ts


contentHeader: string;
contentDescription: string;

constructor()
this.contentHeader = "Dashboard";
this.contentDescription = "This is your dashboard";



starter-content.component.html


<h1>
pageHeader
<small>description</small>
</h1>



starter-content.component.ts


pageHeader: string;
description: string;

constructor(
private gp: GeneralparametersService
)

ngOnInit()
this.pageHeader = this.gp.contentHeader;
this.description = this.gp.contentDescription;



starter-left-side.component.ts


setContent(header, description)
this.gp.contentHeader = header;
this.gp.contentDescription = description;



starter-left-side.component.html


<li class="active"><a href="avascript:void(0);" (click)="setContent('Camera', 'Using Camera')" [routerLink]="['camera']"><i class="fa fa-link"></i> <span>Camera</span></a></li>



Thank you very much for your help.





if you console are you getting any value?
– Chellappan
Aug 10 at 4:10





When I click the link for the camera, I can print the words. It's just not being rendered in the starter-content.html
– Ibanez1408
Aug 10 at 4:10





2 Answers
2



Use a Subject or BehaviorSubject in your Service. Thus, all components get updated when the value changes:



generaParameters.service.ts


import BehaviorSubject, Observable from 'rxjs';

contentHeader: BehaviorSubject<string> = new BehaviorSubject('Dashboard');
contentDescription: BehaviorSubject<string> = new BehaviorSubject('This is your dashboard');

constructor()

public getContentHeader(): Observable<string>
return this.contentHeader.asObservable();


public setContentHeader(value: string): void
this.contentHeader.next(value);


public getContentDescription(): Observable<string>
return this.contentDescription.asObservable();


public setContentDescription(value: string): void
this.contentDescription.next(value);



starter-content.component.html


<h1>
pageHeader
<small>description</small>
</h1>



starter-content.component.ts


pageHeader: string;
description: string;

constructor(
private gp: GeneralparametersService
)

ngOnInit()
this.gp.getContentHeader().subscribe(value =>
this.pageHeader = value;
);

this.gp.getContentDescription().subscribe(value =>
this.contentDescription = value;
);



starter-left-side.component.ts


ngOnInit()
this.gp.getContentHeader().subscribe(value =>
this.pageHeader = value;
);

this.gp.getContentDescription().subscribe(value =>
this.contentDescription = value;
);


setContent(header, description)
this.gp.setContentHeader(header);
this.gp.setContentDescription(description);



starter-left-side.component.html


<li class="active"><a href="avascript:void(0);" (click)="setContent('Camera', 'Using Camera')" [routerLink]="['camera']"><i class="fa fa-link"></i> <span>Camera</span></a></li>





Here you go. Please, don't forget to mark this answer as solution, if it solved your problem or answered your question.
– Lynx 242
Aug 10 at 4:20





It did answer my question. I just need to study on these topics "import BehaviorSubject, Observable from 'rxjs'; contentHeader: BehaviorSubject<string> = new BehaviorSubject('Dashboard'); contentDescription: BehaviorSubject<string> = new BehaviorSubject('This is your dashboard');". But, thank you so much.
– Ibanez1408
Aug 10 at 4:23





You‘re welcome.
– Lynx 242
Aug 10 at 4:49



Since you are communicating using a service you can propagate your changes using an Subject


Subject



When you make changes to your subject via the gp.setContent since your other component is observing the changes they will be automatically updated.


gp.setContent



I used pluck so that we can only take the properties we need and render them separately.



See my implementation. Hope it helps!!!



starter-left-side.component.html


<li class="active"><a href="javascript:void(0);" (click)="gp.setContent('Camera', 'Using Camera')" [routerLink]="['camera']"><i class="fa fa-link"></i> <span>Camera</span></a></li>



generaParameters.service.ts


import Subject from 'rxjs';

private mycontent$ = new Subject();

public content$ = this.mycontent$.asObservable();

setContent(header, description)
this.content$.next(header, description);



starter-content.component.ts


import pluck from 'rxjs/operators';
ngOnInit(): void
this.pageHeader$ = this.gp.content$.pipe(pluck('header'));
this.pageDescription$ = this.gp.content$.pipe(pluck('description'));



starter-content.component.html


<h1>
async
<small>pageDescription$ </small>
</h1>





This is quite good but Lynx's coding is easier to read. Thank you anyways.
– Ibanez1408
Aug 10 at 4:54





I am happy it helped! You are most welcome
– Nico
Aug 10 at 4:56






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard