question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Tabs does not respect setting selectedIndex

See original GitHub issue

We have an md-tab-group with a number of md-tabs and we’re setting an initial selected tab using the selectedIndex property and the user can then select to reset back to that initial tab. The problem we’re finding is that if the user navigates using the labels to other tabs then chooses to reset back to the initial tab it doesn’t work.

We’ve traced through and we can see that selectedIndex is never updated by the tab component when the user selects other tabs, so when we try to set selectedIndex back to the initial value, the angular change detection kicks in and says nothing changed.

We know this is early days but is this behaviour expected to change? Can we submit a PR to fix it? Or is there a rationale for it being this way?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

25reactions
robertmesserlecommented, Jun 15, 2016

@iainmckay I’m working on a fix for this, but wanted to explain why the bug exists. When you initially include tabs, you use a one-way binding in order to pass your selectedIndex into the tabs component: <md-tab-group [selectedIndex]="selectedIndex">.

When you click around inside of tabs, this changes the internal value for selectedIndex, but since this is only a one-way binding, your component’s value is never updated.

When you attempt to change tabs by setting your component value for selectedIndex, you are setting it to the same value that it already has, which is why change detection does not detect any changes.


In order to fix this, currently, you would have to keep a reference to the tab-group in your component and set its value explicitly:

<md-tab-group [selectedIndex]="selectedIndex" #tabGroup> ... </md-tab-group>
<button (click)="tabGroup.selectedIndex = selectedIndex">Reset index</button>

Another option (which is what I am working on adding) is to use a two-way binding so that your local selectedIndex will be updated when the internal value changes. This would look like this:

<md-tab-group [(selectedIndex)]="selectedIndex"> ... </md-tab-group>
<button (click)="selectedIndex = 0">Reset index</button>

Hopefully that makes sense.

10reactions
acedigibitscommented, Sep 2, 2017

why cant you put this in documentation? Getting activeTabIndex is most basic requirements for tabs.

Somehow i find that documentation of google is always incomplete.

Read more comments on GitHub >

github_iconTop Results From Across the Web

`selectedIndex` only sets the selected tab for the first time
The issue occurs because the same variable is mutated simultaneously by the Tabs component and the code in the onClick event handler. The ......
Read more >
TabControl.SelectedIndex Property (System.Windows.Forms)
Gets or sets the index of the currently selected tab page. ... The default is -1, which is also the value if no...
Read more >
How can I change a tab without causing selectedIndexChange
What I want is that the tab will change on every button click but the event handler that is attached to the selectedIndexChange...
Read more >
Tabstrip selected index issue in Kendo UI for jQuery - Telerik
Hi Telerik Team, Here I have got three tabs. I want to get tabindex while selecting each tab and based on this tabindex...
Read more >
Radzen Tab does not output correct selected index
This causes problems because it forces the developer to ensure that none of the tabs are set to selected after every time they...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found