1
這是使用Angular2,我不使用任何文件路徑在我的角度fronEnd我建立一個API來獲取與服務JSON數組對象從laravel 5.4後端數據我的第一個項目:* ngFor在一個頁面中獲取JSON數據數組對象
getFoodsByCat(id :number): Observable<any> {
return this.http.get('http://livrer/api/menu/' +id)
.map(
(response: Response) => { return response.json().foods;
}
);}
在我的部分我用這個服務這樣的:
import {Response} from '@angular/http';
import { Food } from "./food.interface";
import { FoodService } from "./food.service";
export class FoodsComponent implements OnInit {
@Input() food : Food ;
foods: Food[];
constructor(private foodService: FoodService) { }
ngOnInit() {
onGetFoodsByCategorie(id){
this.foodService.getFoodsByCat(id)
.subscribe(
(foods: Food[])=> this.foods = foods,
(error : Response)=> console.log(error)
);
} }
這項服務功能得到這樣的JSON響應:
{"foods":[{"id":10,"designation":"pannin fromage","description":"
Pannini fromage cornichon<\/p>","prix":25,"created_at":"2017-03-11 17:59:34","updated_at":"2017-03-11 ...}]}
在我使用的onload的仿羔皮呢模板開始獲取數據的第一個元素ID = 1
<ul class="resp-tabs-list">
<li class="active" (load)="onGetFoodsByCategorie(1)">Panninis</li>
<li (click)="onGetFoodsByCategorie(2)">Somthing</li>
<li (click)="onGetFoodsByCategorie(3)">Sandwichs</li>
<li (click)="onGetFoodsByCategorie(4)">Pizzas</li>
</ul>
<li *ngFor= "let food of foods" [food]="foods">
//some style and data
</li>
我得到這個錯誤:
Error: Template parse errors:
Can't bind to 'food' since it isn't a known property of 'li'. ("
<li *ngFor= "let food of foods" [ERROR ->][food]="foods">
<div class="item">
<div class="thumbnail-menu-modern">
"): [email protected]:32
你的api調用成功了嗎?它返回的數據沒有任何錯誤?你用console.log()試過了嗎? –
錯誤是很清楚的。清除列表項目中的[食物] =「食物」。我想你可以使用ngFor重複的食物,並顯示在列表項的值。 – digit
是我的api稱爲成功例子ongetfoodcat(1)I rget respnse json:{「foods」:[{「id」:10,「designation」:「pannin fromage」,「description」:「 Pannini fromage cornichon <\/p> 「,」prix「:25,」created_at「:」2017-03-11 17:59:34「,」updated_at「:」2017-03-11 ....}]} – MedKostali