I am new to Angular2, but I already made 30+ components and services and used Input, Output, ChangeDetectorRef, Pipes. So I almost know about Angular2. My app.component.ts is as follows
import { Component } from '@angular/core'; @Component({ selector: 'soc', template: ` <header-block></header-block> <router-outlet></router-outlet> `, }) export class AppComponent { }
Since I need my header block on every page, so I added it outside the router block, so I don't need to add it everywhere. Now the problem I am facing is changing the value of the variable in the header block after completing the action inside the router.
For example, I have a menu that will be visible to the user only after logging in.
{{ user_is_logged }} <ul class="nav navbar-nav navbar-right" *ngIf="user_is_logged == '1'"> <li><a href="#" (click)="logout($event)">Logout</a></li> </ul>
user_is_logged is a variable whose value is received from localStorage in header.block.component.ts
user_is_logged:string = localStorage.getItem("logged");
If the user is not registered, that is, user_is_logged has the value undefined for home.component.ts (where the login logic is written), I ask the user to log in and, after successful login, update the value of the user_is_logged variable, as well as updating the localStorage value of the variable, and then triggering change detection using ChangeDetectionRef (cdr) and then routing to the user profile
this.user_is_logged = "1"; localStorage.setItem('logged', '1'); this.cdr.detectChanges(); this.router.navigate(['u/'+user_id']); //user_id is from login response from server
The problem is that after successfully logging into the user profile, the value {{user_is_logged}} is never updated, even when I use this variable myself or using the value from localStorage or even causing a discovery.
Is it possible to do so, or do I need to add a title block to each page separately? Please ask for more information if I missed something.
EDIT =============>
So, going through the Subject, BehaviourSubject, AsyncSubject, ReplaySubject, Observables topic and that I just couldn't get it to work. This is the last code I wrote:
home.page.component.ts (where the login occurs and should issue an event)
import { HomeService } from '../services/home.service'; // No observable or subject imported here. login() { // Sending status 1 to HomeService this.HomeService.changeStatus("1"); }
home.service.ts (which is imported by both components)
import { BehaviorSubject } from 'rxjs/BehaviorSubject'; // I imported Subject, BehaviourSubject, Observables but cant get it to work export class HomeService { // This looked more reliable than Subject so used this; init with value "0" public subjectStream = new BehaviorSubject<string>('0'); constructor( ){ this.subjectStream.subscribe(value => { // Successfully logs value "1" sent by changeStatus below console.log(value); }); } changeStatus(value: any){ // Gives value "1" when called by home.page.component.ts console.log(value); // Looks correct because it successfully sent value to construct this.subjectStream.next(value); } }
header.block.component.ts
import { HomeService } from '../services/home.service'; // No observable or subject imported here also. constructor( ){ this.HomeService.subjectStream.subscribe(value => { // Logs value "0" at beginning but never logs value "1" console.log(value); }); }
My magazine
// logged by constructor when imported by header.block.component home.service.ts:31 0 // logged by constructor of header.block.component itself header.block.component.ts:25 0 // logged by constructor when imported by home.page.component (I think) home.service.ts:31 0 // =================== // Login called here // =================== // logged by changeStatus after login after called by home.component home.service.ts:36 1 // logged by constructor after getting event from changeStatus home.service.ts:31 1
What am I missing in header.block.component.ts? Since the value is successfully updated inside home.service.ts, but never goes into the header.