1

i am trying to pass data between two routes called Teacher and Student.Both routes are taking the same components with another views.

i have issue to pass booleans from teacher to student i was building a services like this:

service :

 import {Injectable} from "@angular/core";  
 import { BehaivorSubject, Observable } from 'rxjs';
@Injectable({
  providedIn: "root",
})
export class MainServicesService {
private myBbsubject = new BehaviorSubject<boolean> (false);
  myBsObs = this.myBbsubject.asObservable();
constructor(){}
 Camera( isCamera: boolean) {
 this.myBbsubject.next(isCamera)
}
}

student:

import { Component, OnInit} from '@angular/core';
import { MainServicesService } from 'src/app/services/main-services.service';

@Component({
  selector: 'app-teachers',
  templateUrl: './teachers.component.html',
  styleUrls: ['./teachers.component.css']
})
export class StudentComponent implements OnInit,OnDestroy {

  constructor(private mainService: MainServicesService) {

  }
  ngOnInit(): void {
    this.mainService.myBsObs.subscribe(profile =>console.log('teachers',profile));
   
    }

   
  
}

Teacher:

    import { Component, OnInit} from '@angular/core';
    import { MainServicesService } from 'src/app/services/main-services.service';
    
    @Component({
      selector: 'app-teachers',
      template:`<button (click)="Camera()">Camera</button>`,
      styleUrls: ['./teachers.component.css']
    })
    export class TeachersComponent implements OnInit,OnDestroy {
    
      constructor(private mainService: MainServicesService) {
    
      }
      ngOnInit(): void {
        this.mainService.myBsObs.subscribe(profile =>console.log('teachers',profile));
       
        }
    
       
  Camera(){
     this.isCamera = !this.isCamera;
     this.mainService.Camera( this.isCamera);

  }
      
    }

when i cliked on Camera's button the Subject send my a boolean direct to Camera's function and the behaivorSubject triggered the next state. But the of my router module route don't work as expected .the student component take the false as default and not make the update like the teacher make it ; (teacher change the state of the subscrition to true but the subscription from school dont make the update inside of subscrition ) i was triying without (in the app.component.ts like components and work fine but with the router-outlet don't work ) how might i fix this behaivor ...?

4

0 回答 0