In an effort to pass a variable into app.component.ts from another component I created a service (see Global.service.ts below.)
This works well when my nav.component.ts calls the service. However, it doesn't work in app.component.ts. I don't understand why, as the code is the same (see app.component.ts below.)
Nav.component.ts is in a module (global.module.ts). global.module.ts has been added to the imports [] in app.module.ts.
Global.service.ts
@Injectable()
export class GlobalService {
private handleError: HandleError;
isOpen = false;
private _change: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
constructor(
private http: HttpClient,
httpErrorHandler: HttpErrorHandler
) {
this.handleError = httpErrorHandler.createHandleError('GlobalService');
}
get change(): Observable<boolean> {
return this._change.asObservable();
}
toggle() {
this.isOpen = !this.isOpen;
this._change.next(this.isOpen);
}
}
app.component.ts
import { Component, OnInit } from '@angular/core';
import { GlobalService } from './globals/global.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
isOpen = true;
constructor(
private _globalService: GlobalService,
) {
}
ngOnInit(): void {
console.log("foo bar");
this._globalService.change.subscribe(isOpen => {
this.isOpen = isOpen;
console.log(this.isOpen);
});
}
}
nav.component.ts
ngOnInit(): void {
//
this.getNav();
this._globalService.change.subscribe(isOpen => {
this.isOpen = isOpen;
this.NavToggle(this.isOpen);
});
}
//Toggle
hideShowSidebar() {
this._globalService.toggle();
}