如何将用户 ID 从模板传递到函数以获取 angular 中的值 5

How to pass USER ID from template to function to get values in angular 5

我想在单击编辑时将我的用户 ID 传递给服务。

 <tr *ngFor="let visit of visits" style="border-top: 1px solid black">
          <td>{{ visit?.ID }}</td>
          <td>{{ visit?.username }}</td>
            <td>{{ visit?.height }}</td>
            <td>{{ visit?.weight }}</td>
            <td>{{ visit?.patientnote }}</td>
            <td>{{ visit?.doctornote }}</td>
            <td>{{ visit?.nursenote }}</td>
            <td><button mat-raised-button color="accent" (click)="edit()">EDIT</button></td>
            <td><button mat-raised-button color="warn" (click)="delete()">DEL</button></td>

如果你想在 url 中传递值,你必须使用 Angular 路由,如下所示

import {Component} from '@angular/core';
import {Component} from '../Module/Component';
import {ListComponent} from "../Module/ListComponent";

export const ApplicationRoutes = [
{ path: 'Rpt/:id', component: Component },//allows to pass id in route
{ path: 'RptList', component: ListComponent },
{ path: '', component:ListComponent }
];

如果不知道路由,您可以检查路由:https://angular.io/guide/router

下面会起作用

<button mat-raised-button color="accent" 
     (click)="edit(visit?.ID)">EDIT</button>

在ts文件中函数需要带参数,你也可以把arugment设为可选

edit(id?:any)
{}

您必须将 id 作为函数的参数传递:

<td><button mat-raised-button color="accent" (click)="edit(visit.ID)">EDIT</button></td>