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?
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With