简体   繁体   中英

How to return an observable method in another observable Angular 7

In component service class override base-service get method using angular7. I want return mapped data if need to map data, else return origin data. This is my function:

read(isSelectable:boolean): Observable<IFailureMode> {
  const observable = new Observable<IFailureMode>(subscriber => {
      if (isSelectable) {
        return super.read(filter).subscribe(data=>mapToSelectableModel(data));// Here want to return mapped data
      } else {
        return super.read(filter);//Here want to return origin data
      }
    });
  return observable;
}

How can return super.read(filter) as observable.next? Is there more useful method for do it?

Corrected answer

If all you want is to apply a mapping to super.read depending on a provided argument, this should do it:

read(isSelectable) {
  return super.read(filter)
    .pipe(map(data => isSelectable ? mapToSelectableModel(data) : data));
}

Original answer (incorrect)

If I understand your question correctly, this would be an approach for you:

read(fitler) {
  return of(filter)
    .pipe(
      map(f => {
        if (f.key === 'isSelectable' && f.value === 'true') {
          return super.read(f).pipe(map(data => mapToSelectableModel(data));
        } else {
          return super.read(f);
        }
      })
    );
}

In this case, the super call will be mapped conditionally

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM