I am new to Angular, I am trying to pass a value from one screen to another. The thing goes like this: On the first screen I select a "select" value (it's the id of a client), then I click on a button and it redirects me to another page where I need the client's id to be able to work. how can I do this? I have seen forms with input (but they are 2 different screens) and with observables (the id of my client is not saved) but they do not work for me, because I think it is necessary that the 2 screens are active at the same time.
I show you my service:
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
@Injectable()
export class CommunicationService {
private idCliente = new BehaviorSubject<number>(1232);
cast = this.idCliente.asObservable();
constructor() { }
editidCliente(newCliente)
{this.idCliente.next(newCliente);
}
}
On the first screen it changes the value and changes it correctly, but when I speak the second it always shows me the value with which I initialize in this case "1232".
So I'm using it on the first screen:
import { CommunicationService} from './communication.service';
@Component({
selector: '...',
templateUrl: "....",
styles: [],
providers : [...,CommunicationService],
})
export class RegistrarPropuestaComponent implements OnInit {
...
public idCliente : any ;
....
constructor(... private communicationService : CommunicationService) { }
ngOnInit() {
....
this.communicationService.cast.subscribe(cliente => this.idCliente=cliente)
...
}
changeidCliente(idCliente){ //esto se ejecuta en el change del select
console.log("valor del idCliente despues del cambio en el select: " + idCliente);
this.communicationService.editidCliente(idCliente);
}
and so on my second screen, which is where I want to get the idClient value to be able to work.
import { CommunicationService} from './communication.service';
@Component({
selector: '...',
templateUrl: "....",
styles: [],
providers : [...,CommunicationService],
})
export class RegistrarPropuestaComponent implements OnInit {
...
public idCliente : any ;
....
constructor(... private communicationService : CommunicationService) { }
ngOnInit() {
....
this.communicationService.cast.subscribe(cliente => this.idCliente=cliente)
...
}
As you will see, it is almost identical.