1

I have an object being returned with a Promise.

My Object is structured like so:

export class ShoppingCart {
Cart_Items: [
    {
        Id: number,
        SKU: string,
        Link: string,
        ImageURL: string,
        Title: string,
        Description: string,
        Qty: number,
        Unit_Wt: number,
        Unit_Price: number,
        Sub_Total: number,
        Sub_Wt_Total: number,
        IsTaxExempt: boolean
    }
    ];
Shipping_Cost: number;
Taxes: number;
Discount: number;
Sub_Total: number;
Total_Wt: number;
Total: number;

}

I am using this in my component class:

display_cart: ShoppingCart;

constructor(private _shoppingCartService: ShoppingCartService) {}

getShoppingCart() {
    this._shoppingCartService.getShoppingCart().then(display_cart => this.display_cart = display_cart);
    // this.display_cart = this._shoppingCartService.getShoppingCart();
}
ngOnInit(){
    this.getShoppingCart();
}

In My service i am using this to get the data:

getShoppingCart() {
    return Promise.resolve(DISPLAY_CART);
}

The only thing I can use to display any of my data is {{ display_cart | json }} but that just returns my json. How do I extract the values and display the Cart_Items in a loop and the other variables where I need them?

1

1 Answer 1

1

You could use something like that:

display_cart: ShoppingCart;

constructor(private _shoppingCartService: ShoppingCartService) {}

getShoppingCart() {
    this._shoppingCartService.getShoppingCart().then(
    display_cart => {
      this.display_cart = display_cart;
      this.items = display_cart.Cart_Items ;
    });
}

ngOnInit(){
    this.getShoppingCart();
}

And use items in the corresponding template:

<li *ngFor="#item of items">
  {{item.title}}
</li>
Sign up to request clarification or add additional context in comments.

1 Comment

I actually had to change display_cart: ShoppingCart; to display_cart: ShoppingCart = new ShoppingCart; so that even if no data was returned from the server the object was was still defined and didn't cause errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.