I'm coming to you totally desperate ... Since 1 day, I try to pass data between my two component using a service.
I've already use this service in another application and I know that it's work.
But I've try all the things that I've found with Googling : BehaviorSubject, Subject ... But nothing work in my case : The value is changed in the service thanks to the first component, but nothing happens in the second component, even if I'd subscribe to the value in service.
The second component get the value on init, but nothing after ...
Good to know : The second component is the parent of the first component.
This is my code (Actually) :
GlobalFn.Service :
import { Injectable } from '@angular/core'
import { Observable, Subject, BehaviorSubject } from 'rxjs';
@Injectable({
providedIn: 'root',
})
export class globalFnService{
id:any;
activeDeepView = new Subject<boolean>();
constructor() {
this.activeDeepView.next(false);
}
deepView(){
this.activeDeepView.next(!this.activeDeepView);
}
getValue(): Observable<any> {
return this.activeDeepView.asObservable();
}
}
First component (Child) :
import { Component, OnInit } from '@angular/core';
import { globalFnService } from '../services/globalFn.service';
@Component({
selector: 'app-profil',
templateUrl: './profil.component.html',
styleUrls: ['./profil.component.scss'],
providers: [globalFnService]
})
export class ProfilComponent implements OnInit {
constructor(
private globalFn:globalFnService
) {
}
deepView(){
this.globalFn.deepView();
console.log("clicked !");
}
ngOnInit() {
this.globalFn.getValue().subscribe(value => {
if(value == true){
console.log("status",value);
} else {
console.log("status",value);
}
})
}
}
The second component (The parent one) :
import { Component, OnInit } from '@angular/core';
import { globalFnService } from '../services/globalFn.service';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss']
})
export class HomeComponent implements OnInit {
constructor(
private globalFn:globalFnService
) {
this.subscription = this.globalFn.getValue().subscribe(value => {
if(value == true){
console.log("status",value);
} else {
console.log("status",value);
}
})
}
}
If you can find where I do a mistake, you are in capacity to save my day ...
Thank you !
providers: [globalFnService]