[英]How to send instance of ngFor to ngTemplate and pass into function?
我有 ngFor 循環迭代 Cars 數組的元素,如下所示
Cars=[{id:3,name:"Toyota"},{id:8,name:"Hyundai"},{id:12,name:"Lincoln"}]
<div *ngFor="let car of Cars">
{{car.name}}
<ng-container *ngTemplateOutlet="indicator;context:car"></ngcontainer>
</div>
每輛車附近也會有一個按鈕,通過點擊,它會請求 api。不要對其他組件(如汽車)重復我的代碼。我想用 ng-template 將它存儲在頁面上,並在需要時調用它幾次
<ng-template #indicator let-car='car'>
<button (click)="buyCar(car)">Buy</button>
</ng-template>
這不起作用,據我所知,我不能直接在#indicator 模板中使用汽車實例。我可以做一些類似 let-id='id' 的事情,然后可以為 car.id 分配 ID 名稱。但是在 function buyCar() 我需要發送汽車實例,以免破壞其他功能和 ifs。正如我提到的那樣,有沒有辦法讓它與實例一起工作?
@Timcin Cicek,
正如 Document 所說, the object's keys will be available for binding by the local template let declarations.
所以這不可能你試圖通過buycar()
function。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.