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

98
Views
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 answers
Answer question

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 Report

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 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!