如何在角形材料表中调用用户定义的方法?

问题描述 投票:0回答:1

如何在角形材料表中调用用户定义的方法?我正在使用类对象而不是接口,并且在我调用该方法时无法使用。我能够显示属性,但不能显示方法输出。

这是代码段。

export class User implements IUser {

constructor(public id:number, public email: string,
  public first_name: string, public last_name: string, public avatar: string) {
    console.error("Construcor Eroor.")
  }

  upper(): string {
    console.log(this.email.toUpperCase())
    return this.email.toUpperCase();
  }
}



<ng-container matColumnDef="upper">
    <mat-header-cell *matHeaderCellDef>
        upper
    </mat-header-cell>
    <mat-cell *matCellDef="let user ">
        {{user.upper}} <!-- calling a method in the User class -->
    </mat-cell>
</ng-container>

这里是完整的stackblitz链接。https://stackblitz.com/edit/angular-material-reactive-table-ddcsq4

typescript angular2-template
1个回答
0
投票

这里是修改后的代码:

app.component.html:

<hello [name]='name'></hello>
<app-sample></app-sample>

使用上面编辑的代码,您将可以看到name为Angular:enter image description here

希望这会有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.