简体   繁体   English

如何使用 angular 材料标签在 angular 中获取标签并将其设置为默认值

[英]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.

相关问题 如何隐藏所有角度材质选项卡的选项卡内容 - How to hide tab content of all the angular-material tabs Angular Material mdTabs - 如何只获取标签动画效果而不是内容? - Angular Material mdTabs - how to get only the effect of tabs animation not the content? 如何自定义类似于Angular材质选项卡的Angular Ui引导选项卡 - how to customize Angular Ui bootstrap tabs similar to angular material tabs 角度材质选项卡和自动完成 - Angular Material Tabs and autocomplete selectedIndex 不适用于 mat-tab-nav-bar 角度材料选项卡 - selectedIndex is not working for mat-tab-nav-bar angular material tabs 如何使用Angular Material打开选项卡上的菜单? - How to open the menu on tabs using Angular Material? 如何在角度材料选项卡上添加超链接? - How to add a hyperlink on angular material tabs? 使用 Tab 键将焦点设置在有角度的材质 Tab 上 - Use Tab key to set focus on an angular material Tab 在角度引导选项卡中,如何取消选项卡更改? - In angular bootstrap tabs, how to cancel tab change? angular ui bootstrap选项卡组件:未单击选项卡处于活动状态时如何获取选项卡内容 - angular ui bootstrap tabs component: how to get the content of a tab when the tab is active not clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM