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 ...?

hamlet
  • 21
  • 2
  • I think you made a mistake in your copy/pasting because both of your components are called `TeachersComponent` . The thing is, your studentsComponent is not subscribed yet (because it is not loaded in the view) when your observable emit something in TeachersComponent. So it will not get the last emission. – Quentin Fonck Oct 16 '20 at 12:53
  • i was writing by my hand because there are more code inside of my service . But i think the ideas is clear .two components one called TeacherComponent and another StudentComponent inside of don't work .Thank you for answer me . – hamlet Oct 16 '20 at 16:15
  • Can you please reproduce this issue on stackblitz.com? – smtaha512 Oct 24 '20 at 18:52

0 Answers0