Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

268
Views
Cuando pasa un elemento seleccionado a través de un servicio compartido entre un componente, ¿cómo responder a todos los cambios, no solo en Init?

El objetivo de Mg es cuando selecciono un elemento del menú desplegable, que está en un componente de botón, luego pasa el elemento a un componente de tabla a través de un servicio compartido. Necesito esto para cambiar la tabla simplemente mostrando el elemento seleccionado. Ahora, si selecciono el elemento, cambia los datos del servicio al elemento seleccionado, pero el componente de la tabla verifica los datos del servicio en Init (cuando aún no se ha seleccionado nada), y si selecciono un elemento, no vuelve a verificar. Solo una vez en init. ¿Qué debo hacer para que el componente de la tabla mire constantemente si los datos del servicio cambiaron? Pensé en observable, pero no sé cómo implementarlo en mi código.

mi servicio:

 import { Injectable } from '@angular/core'; @Injectable({ providedIn: 'root', }) export class SharedDataService { selectedData: object = {}; constructor() {} setSelectedData(data: object) { this.selectedData = data; } getSelectedData() { return this.selectedData; }

Componente del botón:

 onSelect(company: any) { for (var i: number = 0; i < this.items.length; i++) { if (this.items[i].id == company.target.value) { this.selectedCompany = this.items[i]; this.sharedData.setSelectedData(this.selectedCompany); //pass data to service } } }

Componente de tabla:

 ngOnInit(): void { this.onGetItems(); this.selectedCompany = this.sharedData.getSelectedData(); if (Object.getOwnPropertyNames(this.selectedCompany).length === 0) { //if nothing was selected //do nothing } else { //refresh the table with displaying that one item only } }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

En lugar de simplemente obtener y establecer una variable en un cambio de servicio para usar el sujeto de comportamiento de rxjs. Entonces, con esto, puede suscribirse a la variable en el servicio donde puede escuchar todos los cambios.

Puede encontrar un ejemplo similar aquí: https://stackoverflow.com/a/57355485/7203750

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!