NgIf async pipe, bound as, with condition on emitted value
See original GitHub issueI’m submitting a … (check one with “x”)
[ ] bug report => search github for a similar issue or PR before submitting
[x] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Conditions on a value bound with *ngIf as would be nice.
For this example:
- You have an
Observable<any[]>that emits a list, which can be empty. - You want to conditionally show content when the list is not empty.
Current behavior
Currently, in Angular 4.0.2, you need to do something like this.
<ng-container *ngIf="myList$ | async as myList">
<ng-container *ngIf="myList.length">
<!-- content to show -->
</ng-container>
</ng-container>
Filtering empty list values from the Observable won’t work because you want an emitted empty list value to remove the content.
Desired behavior
It would be nice to avoid the double *ngIf and have a single expression that does this.
Maybe something like this?
<ng-container *ngIf="myList$ | async as myList && myList.length">
<!-- content to show -->
</ng-container>
EDIT: Although that syntax implies a boolean value gets assigned. So maybe some other syntax…
as myList ??? myList.lengthas myList; ?myList.lengthas myList if (myList.length)where myList.length could be any boolean expression
What is the motivation / use case for changing the behavior?
Simpler templates.
- Angular version: 4.0.2
- Language: TypeScript 2.2.2
Issue Analytics
- State:
- Created 6 years ago
- Reactions:50
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Angular async pipe in *ngIf - handle 0 value - Stack Overflow
You wanted *ngIf to show div when promise returns number 0. This is what happnes. 0 is emitted and div will show. When...
Read more >Angular Reactive Templates with ngIf and the Async Pipe
As we can see, we can specify in ngIf an else clause with the name of a template, that will replace the element...
Read more >Angular Async Data Binding with ngIf and ngElse
First, we are using a traditional *ngIf in combination with the async pipe to show our element if the user has loaded. Next...
Read more >Handling Observables with NgIf and the Async Pipe
In this article you'll learn how to use Observables with Angular's NgIf, using the async pipe and practices.
Read more >AsyncPipe - Angular
The async pipe subscribes to an Observable or Promise and returns the latest value it has emitted. When a new value is emitted,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

They’re related as far as my example. Should the empty list
[]be considered truthy within*ngIf?But aside from lists, there could be situations where people want to do quick conditional. Especially if it’s an object. This is contrived, but I think this would be useful in actual projects.
It’s truthy in JavaScript, so yes, it should. Having Angular behave differently here would be a great source of confusion and pain.