Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tabs does not respect setting selectedIndex #687

Closed
iainmckay opened this issue Jun 15, 2016 · 5 comments · Fixed by #702
Closed

Tabs does not respect setting selectedIndex #687

iainmckay opened this issue Jun 15, 2016 · 5 comments · Fixed by #702
Labels
P2 The issue is important to a large percentage of users, with a workaround
Milestone

Comments

@iainmckay
Copy link

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?

@jelbourn
Copy link
Member

Sounds like a bug @robertmesserle

@jelbourn jelbourn added this to the alpha.7 milestone Jun 15, 2016
@jelbourn jelbourn added the P2 The issue is important to a large percentage of users, with a workaround label Jun 15, 2016
@jelbourn jelbourn changed the title Issue with programatically navigating to a tab and user input Tabs does not respect setting selectedIndex Jun 15, 2016
@robertmesserle
Copy link
Contributor

@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.

@iainmckay
Copy link
Author

@robertmesserle Thank you very much for the in-depth response, this was the conclusion we came to also.

@acedigibits
Copy link

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.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 7, 2019
alexeagle pushed a commit to alexeagle/material2 that referenced this issue Nov 6, 2024
…ar#705)

Fixes angular#686 ,closes angular#687, fixes angular#550

- fix docs-api layouts to work down to 360px width
  - fixes Observers, Accessibility, Drag and Drop, Platform, and Overlay layouts
- fix DeprecatedconnectedTo to Deprecated connectedTo display issue
- update footer copyright
- fix exception trying to unsubscribe to undefined routeParamSubscription
- change how sidenav is closed on mobile after selecting a nav item
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
P2 The issue is important to a large percentage of users, with a workaround
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants