Angular:调用插值括号内的函数

bau*_*bei 0 string-interpolation angular

在我的 Angular 项目中,我有一个 A 类:

export class A {
    id: number;

    public displayName(): string{
       return "a";
    }
Run Code Online (Sandbox Code Playgroud)

在我的组件中,我有一个类型为 A 的数组,因此 component.ts 具有以下行:

arrayOfAs: Array<A>;
Run Code Online (Sandbox Code Playgroud)

在 component.html 中,我只想在 ngFor 循环中显示字符串函数的数字和结果:

<div *ngFor="let element of arrayOfAs">
    {{element.id}}, {{element.displayName()}}
</div>
Run Code Online (Sandbox Code Playgroud)

可以显示 id,但对于该函数,我收到错误消息:

类型错误:_v.context.$implicit.displayName 不是函数

如何在插值中使用函数?

显然,这只是我真实案例的简化版本。谢谢

Mic*_*ael 5

您在a.displayName()变量绑定到 name 时调用element。尝试element.displayName()

否则请确保数组包含A注释中建议的类的真实实例