简体   繁体   English

使用插值时无法读取未定义的属性

[英]Cannot read property of undefined when using interpolation

I have put an effort before asking this question to search and make sure this question is not duplicated, but I didn't find anything similar to my issue. 在询问该问题之前,我已进行了努力,并确保没有重复此问题,但是没有找到与我的问题相似的东西。

I am newbie to Angular 2, and my component class is like below: 我是Angular 2的新手,我的组件类如下:

import {Component} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';

@Component({
 selector: 'pm-products',
 templateUrl: 'app/products/product-list.component.html'
})

export class ProductListComponent{
pageTitle: string = 'Product List';
products: any[] = [   
    {
        "productId": 1,
        "productName": "Leaf Rake",
        "productCode": "GDN-0011",
        "releaseDate": "March 19, 2016",
        "description": "Leaf rake with 48-inch wooden handle.",
        "price": 19.95,
        "starRating": 3.2,
        "imageUrl": "SOME URL"
    },
    {
        "productId": 2,
        "productName": "Garden Cart",
        "productCode": "GDN-0023",
        "releaseDate": "March 18, 2016",
        "description": "15 gallon capacity rolling garden cart",
        "price": 32.99,
        "starRating": 4.2,
        "imageUrl": "SOME URL"
    }

    ];
}

And below code is my view: 下面的代码是我的看法:

<div class='table-responsive'>
    <table class='table' 
        *ngIf='products && products.length'>
        <thead>
            <tr>
                <th>
                    <button class='btn btn-primary'>
                        Show Image
                    </button>
                </th>

                <th>Product</th>
                <th>Code</th>
                <th>Available</th>
                <th>Price</th>
                <th>5 Star Rating</th>
            </tr>
        </thead>
        <tbody>
            <tr *ngFor='let product of products'></tr>
            <td></td>
            <td>{{ product.productName }}</td>
            <td>{{ product.productCode }}</td>
            <td>{{ product.releaseDate }}</td>
            <td>{{ product.price }}</td>
            <td>{{ product.starRating }}</td>

        </tbody>
    </table>

</div>

When I run this code I get an error saying "Error in app/products/product-list.component.html:38:16 caused by: Cannot read property 'productName' of undefined", and "Cannot read property 'productName' of undefined" 当我运行此代码时,出现一条错误消息,提示“ app / products / product-list.component.html:38:16中的错误,原因是:无法读取未定义的属性'productName'”和“无法读取以下属性的'productName'未定义”

Using the interpolation in my view, my expectation is to see the products object properties values within the tags. 在我看来,使用插值法是希望看到标签内的product对象属性值。

You don't need to close your <tr> there. 您无需在此处关闭<tr> Actually you "close" the ngFor loop on the same level and product is undefined. 实际上,您在同一级别上“关闭”了ngFor循环,并且未定义product You have to close is after your <td> cells. 您必须在<td>单元格之后关闭。

This works: 这有效:

<tbody>
    <tr *ngFor='let product of products'>
        <td></td>
        <td>{{ product.productName }}</td>
        <td>{{ product.productCode }}</td>
        <td>{{ product.releaseDate }}</td>
        <td>{{ product.price }}</td>
        <td>{{ product.starRating }}</td>
     </tr>
</tbody>

Use Angular safe navigation operator to check nested properties' path. 使用Angular安全导航运算符检查嵌套属性的路径。 Try this: 尝试这个:

<tbody>
    <tr *ngFor='let product of products'>
        <td></td>
        <td>{{ product?.productName }}</td>
        <td>{{ product?.productCode }}</td>
        <td>{{ product?.releaseDate }}</td>
        <td>{{ product?.price }}</td>
        <td>{{ product?.starRating }}</td>
     </tr>
</tbody>

The only issue with your code is 您的代码唯一的问题是

<tr *ngFor='let product of products'></tr>
<td></td>
<td>{{ product.productName }}</td>
<td>{{ product.productCode }}</td>
<td>{{ product.releaseDate }}</td>
<td>{{ product.price }}</td>
<td>{{ product.starRating }}</td>
-------------------------------------------
<tr *ngFor='let product of products'></tr>

Here you are looping through tr tag , so td tag will never get any value of product , because of that you are getting error of 在这里,您正在遍历tr标签,因此td标签将永远不会获得product任何值,因为这会导致错误

"Error in app/products/product-list.component.html:38:16 caused by: Cannot read property 'productName' of undefined", and "Cannot read property 'productName' of undefined"

If you will inspect element and check the output you will get , 如果您要检查element并检查输出,您将得到,

And there is no need of using any "Elvis operator" , as you are already providing data via component and before view loaded. 而且,您无需使用任何"Elvis operator" ,因为您已经在通过组件且在加载视图之前提供了数据。

Last code should be like 最后的代码应该像

<tr *ngFor='let product of products'>
    <td></td>
    <td>{{ product.productId }}</td>
    <td>{{ product.productCode }}</td>
    <td>{{ product.releaseDate }}</td>
    <td>{{ product.price }}</td>
    <td>{{ product.starRating }}</td>
</tr>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM