[英]How to get tab and set it as default in angular with angular material Tabs
I have a component inside another big component (parent).我在另一个大组件(父组件)中有一个组件。 The big component has the tabs feature and to access those tabs inside the inner component im doing the following:
大组件具有选项卡功能,并且要访问内部组件中的这些选项卡,请执行以下操作:
document.querySelectorAll('.mat-tab-group');
That above code outputs me an array of length 2 because there is another tab.上面的代码输出了一个长度为 2 的数组,因为还有另一个选项卡。 So to access the last one and set the default tab im doing the following:
因此,要访问最后一个并设置默认选项卡,请执行以下操作:
const tabs = document.querySelectorAll('.mat-tab-group');
if (tabs.length > 0) {
tabs[tabs.length - 1]['selectedIndex'] = 3;
}
But that does nothing.但这无济于事。 I appreciate any help.
我很感激任何帮助。 Thank you.
谢谢你。
Note : I cant touch parent component.注意:我无法触摸父组件。 Only the inner one.
只有内在的。
Look at the official documentation: https://material.angular.io/components/tabs/overview看官方文档: https://material.angular.io/components/tabs/overview
Here it says there is an attribute called "selectedIndex" which determines which tab should be chosen.这里它说有一个名为“selectedIndex”的属性,它确定应该选择哪个选项卡。 You dont need no querySelector or any javascript related stuff.
您不需要 querySelector 或任何 javascript 相关的东西。 Here is an example on how to dynmically change the tab just by the selectedIndex value: https://stackblitz.com/angular/xamqklmnbkkj?file=src%2Fapp%2Ftab-group-dynamic-example.ts
这是一个关于如何仅通过 selectedIndex 值动态更改选项卡的示例: https://stackblitz.com/angular/xamqklmnbkkj?file=src%2Fapp%2Ftab-group-dynamic-example.ts
Hope it helps!希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.