无法读取属性"totalPrice"未定义 [英] Cannot read property "totalPrice" of undefined

查看:154
本文介绍了无法读取属性"totalPrice"未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Angular 2模板上有这段代码.

I have this code on my Angular 2 template.

<td class="hidden-xs text-center"><strong>Total: &#8369; {{carts.totalPrice}}</strong></td>

当我在控制台中登录carts.totalPrice时,它显示正确的值.这是对象值.

When I log carts.totalPrice in console, it shows the correct value. Here is the object value.

我在这里怀疑模板的加载速度比对象快,这就是为什么它告诉我无法读取该属性的原因.我说的对吗?

What I suspect here is that the template loads faster than the object that's why it tells me that it cannot read the property. Am I right?

这是我获取购物车价值的方法.

This is my method on getting the carts value.

getItems(){
    this._cartService.getCartItems(localStorage.getItem('currentUserId'))
        .subscribe((cart) => {
            this.cartItems = cart.products;
            // this.totalPrice = cart.totalPrice;
            this.carts = cart;
            console.log(this.carts)
        },
        (err) => console.log(err));
}

错误堆栈跟踪.

推荐答案

尝试一下:

<td *ngIf="carts" class="hidden-xs text-center"><strong>Total: &#8369; {{carts.totalPrice}}</strong></td>

这篇关于无法读取属性"totalPrice"未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆