Home > Mobile >  how to use object in pipe async and in component?
how to use object in pipe async and in component?

Time:07-08

I need to use the reference of observable twice. in html:

<div *ngIf="subject$ | async as subject">

and in template:

this.subject = subject

CodePudding user response:

If you want to get the value in you code, you need to subscribe

this.subject$.subscribe((value) => this.subject = value);

I guess, that this was your intenion; but the alias as subject only exists inside the tag it was defined.

You should use the share()Operator to ensure you do not trigger something twice (like a server request):

private subject$ = new Subject<any>();
public observable$ = this.subject$.map(share());

additional benefit is that observable is now an Observable which can not be used to emit a new value at the "consuming code".

CodePudding user response:

I am not sure, that I understood your question. What is a question?)

The first of all is subject is not equal Observable.

Subject|BehaviorSubject any else subject should be observable if you will convert it.

The simple example In service:

this.subject = new Subject();

public updateSubject(value:any){
this.subject.next(value);
}

public getValue(){
return this.subject.asObservable();
}

In component

this.subject$ = this.service.getValue();

In html:

<div *ngIf="(subject$ | async) as subject">
  • Related