7

I have variable defined like this: myVar: Observable<Observable<MyObject>[]>.

I am using Angular4 feature to enumerate with async pipe

*ngFor="let obsMyObject of (myVar | async)"

now I have Observable of MyObject, but I need just MyObject. I could write something like that:

<div *ngFor="let obsMyObject of (myVar | async)">
    <div *ngIf="let obsMyObject | async; let MyObject"></div>
</div>

but I can not do this in inner div, because I am setting flex order (which doesn't have any effect on inner div) property so I need something like this:

<div *ngFor="let obsMyObject of (myVar | async); let MyObject = (obsMyObject | async)"
    [style.order]="MyObject.order">
</div>

I just had one idea how to do this, and I tried this:

<div *ngFor="let obsMyObject of (myVar | async); let MyObject = obsMyObject)">
    {{MyObject}}
</div>

but MyObject is undefined, so there is no way let MyObject = (obsMyObject | async) would work.

3
  • What should be the difference between obsMyObject and MyObject? Commented Dec 9, 2017 at 14:30
  • Why do you have an observable of an observable? It sounds like you need to rewrite whatever provides it, e.g. to use flatMap rather than map. Commented Dec 9, 2017 at 14:30
  • obsMyObject is BehaviorSubject<MyObject> and as that I can not use it to set style.order property. Commented Dec 9, 2017 at 14:31

1 Answer 1

1

I think you want to use an ng-container and an ngIf to subscribe and create reference to the observable. Then from within the ng-container you can reference this new variable as if it was a plain object.

<ng-container *ngIf="myVar$| async as myVar">
   <div *ngFor="let obsMyObject of myVar">
    {{obsMyObject }}
   </div>
</ng-container>

The use of myVar and object is really confusing. Here's a better example:

Property in component declared as:

users$: Observable<User[]> 

And the template:

<ng-container *ngIf="users$ | async as users">
   <div *ngFor="let user of users">
     {{ user.name }}
   </div>
</ng-container>
Sign up to request clarification or add additional context in comments.

1 Comment

This is not it. I have Observable<Observable<User>[]> which is not Observable<User[]>. I need 2 Observables, because both array of users and user changes.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.