Issue
I have this inside my code:
<div *ngIf="(listCount$| async) > 0 ">
but it won't pass the pipeline because it says
Object is possibly 'null' or 'undefined'.
in ts file:
readonly listCount$ = new BehaviorSubject<number | undefined>(undefined);
any help, can i do this in one line?
Solution
It's an async operation that might result in an undefined value that's why you are getting the error. Provide a default value in case of undefined in your template condition.
<div *ngIf="((listCount$| async) ?? 0) > 0 ">
OR set the strictNullChecks
to false in your tsconfig.json
file.
"strictNullChecks": false
Demo:- https://stackblitz.com/edit/angular-ivy-kheete?file=src%2Fapp%2Fapp.component.html
Answered By - Vimal Patel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.