Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - How to work with value 0 in @if template flow syntax

I have a problem with @if in the Angular template flow syntax.

A value is available in an RxJs Observable. So the async pipe helps and the value is assigned to a variable.

@if (currentPageNumber$ | async; as currentPageNumber) {
// currentPageNumber is number

For value 0 the if statement is not valid. So I exclude only null values... but now the value of currentPageNumber is boolean.

@if ((currentPageNumber$ | async) !== null; as currentPageNumber) {
// currentPageNumber is boolean

How can I check against null but keep my variable with the value of the stream?

like image 536
Lord Midi Avatar asked Oct 27 '25 12:10

Lord Midi


1 Answers

You can wrap the value directly in the template, this is probably the most straight forward way knowing how the AsyncPipe works.

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [AsyncPipe],
  template: `
    @if({value: (sub$ | async)}; as wrappedValue) {
      foo {{wrappedValue.value}}
    } @else {
      bar
    }
  `,
})
export class App {
  sub$ = new BehaviorSubject(0);
}
like image 198
Matthieu Riegler Avatar answered Oct 29 '25 01:10

Matthieu Riegler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!