Angular 5 - Stop errors from undefined object before loading data

后端 未结 4 1048
旧时难觅i
旧时难觅i 2021-02-15 13:57

What\'s the best way to prevent errors in console from objects that are still undefined?

Let\'s say I have this

name : string;
constructor(private data:          


        
4条回答
  •  长发绾君心
    2021-02-15 14:25

    Multiple ways: You can use any one suitable to you.

    1. Adding ngIf : If name is undefined or null or '' it will not render the element and prevent errors in console. When name gets defined value it will automatically update the view.

    *ngIf="name"

    2. Adding async pipe : View will update whenever name gets defined. It waits for name to get defined.

    {{ name | async }}

    3. Adding fallback value : This is simply or condition. If name is undefined or null or '' , you can decide which fallback value to assign . {{ name || "" }}

提交回复
热议问题