Cannot read property of undefined angular2 ngif

前端 未结 3 1460
无人及你
无人及你 2020-12-20 16:26

I am now able to get the object in the view however I cannot run an if statement. Per previous answer this is how I am bringing in the object.

public getPos         


        
3条回答
  •  粉色の甜心
    2020-12-20 16:39

    The cart object is null until the service getPosts$ returns (callback). Therefore, the code *ngIf="cart.vegetable ... is equal to *ngIf="null.vegetable ... until that happens. That is what is happening.

    What you could do is put a DOM element with *ngIf="cart" containing the other *ngIf. For example:

    {{cart.vegetable}}

    *Edit: As it is said in the next answer, a good alternative (and good practice) is the following:

    {{cart.vegetable}}

提交回复
热议问题