簡體   English   中英

如何將參數從html傳遞給typescript function?

[英]How to pass parameters to a typescript function from html?

我以 JSONArray 的形式獲取我的users ,我想在單擊他們的名字時顯示每個用戶的數據。 但我無法將user.id傳遞到 function。

<ng-container *ngFor = "let user of users" >
        <button (onclick)="getdata(user.id)" mat-raised-button color="primary">{{user.name}}</button>
      </ng-container>

它的 angular 使用(click)而不是(onclick)

<ng-container *ngFor="let user of users">
  <button (click)="getdata(user.id)" mat-raised-button color="primary">
    {{ user.name }}
  </button>
</ng-container>
getdata(id) {
  console.log(id);
}

您能否嘗試僅使用 click() 而不是 onclick()

<ng-container *ngFor = "let user of users" >
    <button (click)="getdata(user.id)" mat-raised-button color="primary">{{user.name}}</button>
  </ng-container>

嘗試這個

<ng-container *ngFor = "let user of users" >
    <button (click)="getdata(user.id)" mat-raised-button color="primary">{{user.name}}</button>
</ng-container>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM