Skip to content

Commit 52e5a8d

Browse files
authored
fix(angular): back button goes back to proper tab (#18005)
fixes #17278 fixes #15216
1 parent 0d17e05 commit 52e5a8d

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

angular/src/directives/navigation/stack-controller.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,23 @@ export class StackController {
101101
return Promise.resolve(false);
102102
}
103103
const view = views[views.length - deep - 1];
104-
return this.navCtrl.navigateBack(view.url).then(() => true);
104+
let url = view.url;
105+
106+
const viewSavedData = view.savedData;
107+
if (viewSavedData) {
108+
const primaryOutlet = viewSavedData.get('primary');
109+
if (
110+
primaryOutlet &&
111+
primaryOutlet.route &&
112+
primaryOutlet.route._routerState &&
113+
primaryOutlet.route._routerState.snapshot &&
114+
primaryOutlet.route._routerState.snapshot.url
115+
) {
116+
url = primaryOutlet.route._routerState.snapshot.url;
117+
}
118+
}
119+
120+
return this.navCtrl.navigateBack(url).then(() => true);
105121
});
106122
}
107123

angular/test/test-app/e2e/src/tabs.e2e-spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,25 @@ describe('tabs', () => {
130130
expect(await tab.$('ion-back-button').isDisplayed()).toBe(false);
131131
});
132132
});
133+
134+
describe('enter url - /tabs/contact/one', () => {
135+
beforeEach(async () => {
136+
await browser.get('/tabs/contact/one');
137+
});
138+
139+
it('should return to correct tab after going to page in different outlet', async () => {
140+
const tab = await getSelectedTab();
141+
await tab.$('#goto-nested-page1').click();
142+
143+
await testStack('app-nested-outlet ion-router-outlet', ['app-nested-outlet-page']);
144+
145+
const nestedOutlet = await element(by.css('app-nested-outlet'));
146+
const backButton = await nestedOutlet.$('ion-back-button');
147+
await backButton.click();
148+
149+
await testTabTitle('Tab 2 - Page 1');
150+
});
151+
})
133152
});
134153

135154
async function testState(count: number, tab: string) {

angular/test/test-app/src/app/nested-outlet/nested-outlet.component.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<ion-header>
22
<ion-toolbar>
3+
<ion-buttons slot="start">
4+
<ion-back-button></ion-back-button>
5+
</ion-buttons>
36
<ion-title>
47
NESTED OUTLET
58
</ion-title>

angular/test/test-app/src/app/tabs-tab2/tabs-tab2.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ <h1>Welcome to Tab 2</h1>
99
<p>
1010
<ion-button routerLink="/tabs/account" id="goto-tab1-page1">Go to Tab 1 - Page 1</ion-button>
1111
<ion-button routerLink="/tabs/account/nested/12" id="goto-tab1-page2">Go to Tab 1 - Page 2</ion-button>
12+
<ion-button routerLink="/nested-outlet/page" id="goto-nested-page1">Go to nested</ion-button>
1213
</p>
1314
</ion-content>

0 commit comments

Comments
 (0)