Hi Tillias,

Don't subscribe in your component, but use the async pipe. If you would 
have provided a stackBlit/plucker/... it would be easier to help you.

Here is a (completely untested) sample:
interface Item {
[key: string]: any;
}

@Component({
// tslint:disable-next-line:component-selector
selector: 'app-Some-Element',
template: `
<p>Do something more usefull</p>
<pre>{{items$|async}}</pre>
`
})
class SomeElementComponent {
items$ = this.notify.item$.pipe(
switchMap(() => this.list.getItems()),
tap(r => console.log('incomming', r))
);
constructor(private notify: NotificationService, private list: ListService) 
{}
}

@Injectable({
providedIn: 'root'
})
export class NotificationService {
private _item = new BehaviorSubject<Item>(null);
public item$ = this._item.asObservable();

constructor() {}

notifyNewItemHasBeenCreated(item: Item) {
this._item.next(item);
}
}

@Inject({
providedIn: 'root'
})
export class ListService {
basePath = 'my-api.com';
apiPath = 'item';

constructor(private httpClient: HttpClient) {}

getItems(): Observable<Item[]> {
return this.httpClient.get<Item[]>(this.basePath + '/' + this.apiPath);
}
}


That way you don't need to subscribe in the component at all. The async 
pipe will take care of the subscription

Regards
Sander

-- 
You received this message because you are subscribed to the Google Groups 
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to