简体   繁体   English

使用动态变量的模板表达式中的 Angular getter

[英]Angular getter in template expression using dynamic variable

Assume I have an Angular (6+) component like the following:假设我有一个 Angular (6+) 组件,如下所示:

@Component({
    selector: 'my-component',
    template: '{{test}}'
})
export class MyComponent {
    test = 'myData';

    constructor() {}

    get myData(): string {
        return 'this is what I want so see in the template';
    }
}

This is a very simplistic view of what I'm trying to achieve, but basically, if my template was {{myData}} I would see my getter function being called and output the expected string.这是我试图实现的非常简单的视图,但基本上,如果我的模板是{{myData}}我会看到我的 getter 函数被调用并输出预期的字符串。 But if the getter function name to be called is held in another variable in the component (imagine in my real world scenario that I have an array of these), is there a way to tell Angular to call the respective getter whose name is held by the variable test above?但是,如果要调用的 getter 函数名称保存在组件中的另一个变量中(想象一下,在我的真实世界场景中,我有一个这些数组),有没有办法告诉 Angular 调用其名称由上面的变量测试?

似乎{{this[test]}}正在工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM