question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NgIf async pipe, bound as, with condition on emitted value

See original GitHub issue

I’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.length
  • as myList; ?myList.length
  • as 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:open
  • Created 6 years ago
  • Reactions:50
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

27reactions
arlowhitecommented, Apr 23, 2017

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.

<div *ngIf="myObject$ | async as myObject; if myObject.a && myObject.x > 10">
  <span>{{myObject.a}}</span>
</div>
4reactions
Airbladercommented, Jun 26, 2018

Should the empty list [] be considered truthy within *ngIf?

It’s truthy in JavaScript, so yes, it should. Having Angular behave differently here would be a great source of confusion and pain.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found