Advertisement
msdfib

Untitled

Feb 28th, 2021
606
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Injectable } from '@angular/core';
  2. import { BehaviorSubject, Observable } from 'rxjs';
  3.  
  4. @Injectable({
  5.   providedIn: 'root'
  6. })
  7. export class ToggleService {
  8.  
  9. private showSidebar: BehaviorSubject<boolean>;
  10.  
  11. constructor() {
  12.   this.showSidebar = new BehaviorSubject<boolean>(false);
  13. }
  14.  
  15.   public getTheBoolean(): Observable<boolean> {
  16.     return this.showSidebar.asObservable();
  17.   }
  18.  
  19.   public setTheBoolean(newValue: boolean): void {
  20.     this.showSidebar.next(newValue);
  21. }
  22.  
  23. }
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30. import { Component } from '@angular/core';
  31. import { ToggleService } from '../toggle.service';
  32.  
  33. @Component({
  34.   selector: 'app-navbar',
  35.   templateUrl: './navbar.component.html',
  36.   styleUrls: ['./navbar.component.css'],
  37.   providers: [ToggleService]
  38. })
  39. export class NavbarComponent  {
  40.  
  41.   showSidebar: boolean = false;
  42.  
  43.   constructor(private toggleService: ToggleService) {
  44.   }
  45.  
  46.   ngOnInit() {
  47.     this.toggleService.getTheBoolean().subscribe(showSidebar => this.showSidebar = showSidebar);
  48.   }
  49.  
  50.   toggleSidebar(){
  51.     this.toggleService.setTheBoolean(!this.showSidebar);
  52.     //alert(this.showSidebar)
  53.   }
  54.  
  55. }
  56.  
  57.  
  58.  
  59.  
  60. import { ToggleService } from './../toggle.service';
  61. import { Component, OnInit} from '@angular/core';
  62.  
  63. @Component({
  64.   selector: 'app-sidenav',
  65.   templateUrl: './sidenav.component.html',
  66.   styleUrls: ['./sidenav.component.css']
  67.   providers: [ToggleService]
  68. })
  69. export class SidenavComponent implements OnInit {
  70.  
  71.   showSidebar: boolean = false;
  72.  
  73.   constructor(private toggleService: ToggleService) {
  74.   }
  75.  
  76.   ngOnInit() {
  77.     this.toggleService.getTheBoolean().subscribe(showSidebar => this.showSidebar = showSidebar);
  78.   }
  79.   toggleSidebar(){
  80.     this.toggleService.setTheBoolean(!this.showSidebar);
  81.     //alert(this.showSidebar)
  82.   }
  83. }
  84.  
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement