我已經付出了努力,然後要求此問題進行搜索並確保此問題不重複,但我沒有找到與我的問題類似的任何內容。使用插值時無法讀取未定義的屬性
我是新手到角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"
}
];
}
而下面的代碼是我的看法:
<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>
當我運行這段代碼,我得到一個錯誤,說:「錯誤在app/products/product-list.component.html:38:16引起的:無法讀取未定義的屬性'productName'和'無法讀取未定義的屬性'productName'」
在我的視圖中使用插值,我的期望是看到標籤內的產品對象屬性值。
一個糟糕的錯誤導致了這麼多的混亂!謝謝@Hristo! – Benjamin
它發生了:)不客氣。 –