I have two elements in my angular project that are completely independent. The first one shows a list of license plates and two buttons for each plate: "View current position" and "View positions for the last 24h". Those two buttons redirect to the same component which shows in a map some points depending of a variable "method". I want to set the "method" in a certain value when the button is clicked, but I have no idea how to do this.
I have tried about the Subject/Subscribe thing but did not work for me. It is possible that I did not apply it properly, as I am not very confident about how it works. In the examples I have found, the subject was emmited by some @Injectable, but I am working with two @Component.
Thank you all.
Edit: I will try to explain what I have tried.
I have the service
import { Inject, Injectable } from "@angular/core";
import { MatriculasListadoComponent } from "../components/matriculas-listado/matriculas-listado.component";
import { Metodos } from "../models/metodos";
@Injectable({
providedIn: 'root'
})
export class ConexionDataService {
metodoBoton: Subject<Metodos>
constructor() {
this.metodoBoton = new Subject<Metodos>();
}
}
Then I have a method in my component from where I want to send the data which is called clicking the buttons:
constructor(private matriculaService: MatriculaService, private conexionDataService: ConexionDataService) {
this.dataSource = new MatTableDataSource<Matricula>([]);
}
...
enviarMetodo(metodo:Metodos) {
this.conexionDataService.metodoBoton.next(metodo);
}
And I have this NgOnInit in the component where I want to recieve the data
providers: [ListaPosicionesService, DatePipe, ConexionDataService]
...
ngOnInit(): void {
this.conexionDataService.metodoBoton.subscribe(log=> {
this.metodo=log
console.log(log)
})
I am getting log as "undefined". Probably I am not doing it properly as I do not understand at all how those services work...