Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

101
Visualizações
Angular-Mapping value from NgRx selector into an object to be returned as an Observable

I want to return an observable (line 6) as DataObject. Line 9 is listening to store changes, so when there is a store change - lines 10,11,12 execute but not before store change right? Or Will it get the existing value in the store and execute the block (lines 10,11,12). But if store hasnt changed yet, will line 14 wait until line 10,11,12 execute or will it return line 14 as empty object? What can I do if I want to listen to changes -> map the data from store into DataObject and return it as observable, not before that?

class DataObject {
  propA: string = '';
  propB: string = '';
  propC: SecondDataObject;
}

getDataModel(): Observable<DataObject> {
  let data = new DataObject();
  this.store.pipe(select(appState)).subscribe((val) => {
    const data = new DataObject();
    data.propA = val.propA;
    data.propB = val.propB;
    data.propC = val.propC;
  });
  return of(data);
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can achieve that without subscribing to the selector, just by using the map operator and retuning the Observable itself, like the following:

getDataModel(): Observable<DataObject> {
  return this.store.pipe(select(appState)).pipe(
    map((val) => {
      const data = new DataObject();
      data.propA = val.propA;
      data.propB = val.propB;
      data.propC = val.propC;
      return data;
    })
  );
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You can return a new observable like below :-

getDataModel():Observable<DataObject>()
         {
           let data = new DataObject();
           return new Observable((observer) => {
              this.store.pipe(select(appState)).subscribe((val)=>{
                data.propA = val.propA;
                data.propB = val.propB;
                data.propC = val.propC;
                observer.next(data);
                observer.complete();
              }));
           });     
         }
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda