简体   繁体   English

如何动态访问 angular.html 中定义的模板引用变量(符号:#)

[英]how to dynamically access template reference variables (symbol: #) defined in the angular.html

I am not able to access template reference variables #interface if this.activeTab = 'interface';我无法访问模板引用变量#interface if this.activeTab = 'interface';

//  parent.html
<button type="button" [disabled]="activeTab.revertDisabled"></button>
<interface-settings #interface></interface-settings>
<car #car></car>

// parent.ts
 this.activeTab = 'interface';

//child.ts (interface-settings.ts)
 this.revertDisabled = true;

but if I change it to the following, then it is able to access #interface.但是如果我将其更改为以下内容,则它可以访问#interface。

 <button type="button" [disabled]="interface.revertDisabled"></button>

Is there a way that I can dynamically access the values for 'this.activeTab'?有没有一种方法可以动态访问“this.activeTab”的值?

I have tried the following but doesn't work我尝试了以下但不起作用

 <button type="button" [disabled]="[activeTab].revertDisabled"></button>

How a about using getter如何使用 getter

get revertDisabled(){
   return yourActivetabl.get.somehow.your.value;

}

and

<button type="button" [disabled]="revertDisabled"></button>

If you need to get instance of of interface component as well, use如果您还需要获取接口组件的实例,请使用

@ViewChildren("interface") //this is the #innterface value
instances:QueryList(InterfaceClass);

You can't use the activeTab as reference because activeTab is a string.您不能使用activeTab作为参考,因为activeTab是一个字符串。 Instead you should use brackets notation as you did, so that you can reference to the class member that it's name is the value of activeTab .相反,您应该像以前一样使用括号表示法,以便您可以引用 class 成员,它的名称是activeTab的值。

Add this to before the brackets:this添加到括号前:

<button type="button" [disabled]="this[activeTab].revertDisabled"></button>

Live example: https://stackblitz.com/edit/angular-playground-o1mxe1现场示例: https://stackblitz.com/edit/angular-playground-o1mxe1

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

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