In my header component I am declaring a variable this.userFullName = this.globalService.userFullName;
& then I am using this variable userFullName in my header template . Now when I change my global variable userFullName from another component (Counsellor Component) this.globalService.userFullName = "Rob"
, this change not getting reflected into header template.Why so ? Any alternate solution for implementing this would also be helpful.
Asked
Active
Viewed 95 times
0

MOHIT
- 43
- 1
- 2
- 7
-
how are you using globalService in both the component ? have you included in separately in providers for both ? , if yes , then it wont work. IF possible can you share the @component decorative for both and constructor definition as well. – pritesh agrawal Jul 04 '17 at 04:39
-
For Header Component Component Decorator``@Component({ selector: 'app-header', templateUrl: './header.component.html', styleUrls: ['./header.component.scss'] })`` and constructor definition ``constructor(private globalService : GlobalService ,private router: Router) { this.userFullName = this.globalService.userFullName; }`` ; for cousellor ``@Component({ selector: 'app-counsellor', templateUrl: './counsellor.component.html', styleUrls: ['./counsellor.component.scss'] })`` and constructor definition ``constructor(private globalService : GlobalService)`` – MOHIT Jul 04 '17 at 05:23
-
you can refer this question , it provide exact answer to your question.https://stackoverflow.com/questions/34714462/updating-variable-changes-in-components-from-a-service-with-angular2 – pritesh agrawal Jul 04 '17 at 06:15
-
I am using **Header Component** inside **Counsellor Component** ; So I think I should Reload Header after updating the **userFullName** Value .How to reload component ? – MOHIT Jul 04 '17 at 06:16
-
you dont need to reload the component if you use observable to subscribe for the value , read the first answer in the link , I think it should solve your problem. – pritesh agrawal Jul 04 '17 at 06:22
-
I went through this article [link](http://jasonwatmore.com/post/2016/12/01/angular-2-communicating-between-components-with-observable-subject) and it solved my problem Thanks @priteshagrawal for your useful suggestion. – MOHIT Jul 04 '17 at 08:25