45

Is it possible to assign (areShipsExpanded$ | async) to variable without *ngIf? Because in case that this is flag true/false when I have: *ngIf="(areShipsExpanded$ | async) as flag" then button is not displaying in case of false.

And I would like something like this:

<button *let="(areShipsExpanded$ | async) as flag"
   (click)="expandAllShips(flag)">{{(flag ? "Collapse" : "Expand"}}
</button>
6
  • Why don't you do <button (click)="expandAllShips(flag)">{{(areShipsExpanded$ | async ? "Collapse" : "Expand"}} </button> Commented Jul 27, 2018 at 10:47
  • 1
    (click)="expandAllShips(flag) because of that? Commented Jul 27, 2018 at 10:47
  • 1
    Why not subscribe to areShipsExpanded$ in component and assign it to a flag property? Commented Jul 27, 2018 at 11:07
  • 2
    Sure I can, but I was wondering whether is possible to do it directly from async :) Commented Jul 27, 2018 at 11:09
  • Here's another way from stackoverflow.com/a/40751358/2604813 -- <div *ngFor="let flag of [areShipsExpanded$ | async]"></div> Commented Dec 5, 2020 at 1:33

5 Answers 5

60

Little late to the party here but I found a great example of how to do this from this medium article

You still use *ngIf but you make sure that it always evaluates truthy by passing in a new object with your observable value as a property.

<div *ngIf="{value1: myObs$ | async} as valuesObj">
    {{ valuesObject.value1 }}
</div>

IMO this is still a little hacky but much less hacky than the other answer or creating your own *ngLet directive.

As mentioned in the article, this is also a good way to group together multiple observable values in multiple object properties so you don't have to have multiple async statements or nested <ng-container> elements.

Sign up to request clarification or add additional context in comments.

3 Comments

There's a number of *ngLet implementations out there, but this saved adding another dependency 👍
A little hacky? That's an ugly workaround that abuses the language. I strongly discourage this. Yet again, JavaScript is hacky by definition, that's why we moved to TypeScript a long time ago.
It is awesome!!!
16

You can do it using ng-template and ng-container

<ng-template #myTempl let-flag="areShipsExpand">
      <button
   (click)="expandAllShips(flag)">{{flag ? "Collapse" : "Expand"}}
  </button>
</ng-template>

<ng-container *ngTemplateOutlet="myTempl; context:{areShipsExpand: areShipsExpanded$ | async}"></ng-container>

DEMO

Comments

10

Use the *ngrxLet directive from @ngrx/component

<ng-container *ngrxLet="observableNumber$ as n">
   <app-number [number]="n">
   </app-number>
</ng-container>

Visibility of the element containing the ngrxLet directive is not affected by the value of the observable expression.

1 Comment

This is still in the experimental phase. 2022. Angular 14.
1

In the next angular (v19), we'll be able to use the following alternative:

@let user = user$ | async;

source

Comments

0

I've even used this for binding's value

(someObs$ | async).propName

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.