Is there a parameter to provide to the router so that router.navigate() will open in a new tab/window in browser?
Asked
Active
Viewed 2.0k times
5
-
The router doesn't support that. Why don't you just call `window.open()`? See also http://stackoverflow.com/questions/4907843/open-a-url-in-a-new-tab-and-not-a-new-window-using-javascript – Günter Zöchbauer Dec 12 '16 at 07:57
-
I want to open a router url, not a regular one... Navigate in a new window to whatever the router is configured to. Is that possible? – tigrenok00 Dec 12 '16 at 09:10
-
1That won't work. A different tab is a different application. – Günter Zöchbauer Dec 12 '16 at 09:12
-
you are using routing using `` tag ? routerLink ?? or in `.ts` file using `router.navigate...` ? – Pardeep Jain Dec 12 '16 at 09:52
-
router.navigate in this case – tigrenok00 Dec 12 '16 at 11:09
-
2Purpose of router is to load only necessary parts and render them where
is. If you open it in a new tab, it will bootstrap whole app again anyway. – harunurhan Feb 23 '18 at 10:25
2 Answers
2
Create routes just like instructed here.
Set up your app-routing.module.ts
( or just put it in app.module.ts
), place the <router-link>
in your app.component.html
.
Now, to open a new window from the main page, ( or whatever sub-routes you have created ) just call a function, handled in the main component, that will point to the url of the sub-component route that you setup for the new window.
Example snippits:
app-routing.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { MainComponent } from './main/main.component'; // main window
import { JclInfoComponent } from './jcl-info/jcl-info.component'; // new window/tab
const appRoutes: Routes = [
{ path: '', component: MainComponent }, // main route
{ path: 'openJcl', component: JclInfoComponent } // new window route
];
@NgModule({
imports: [
RouterModule.forRoot( appRoutes )
],
exports: [
RouterModule
]
})
export class AppRoutingModule {}
app.component.html
<router-outlet>
<!-- Just showing you that you can put things inside this. -->
<template ngbModalContainer></template>
</router-outlet>
main.component.html
<button (click)="testMe()">myLabel</button>
main.component.ts
public testMe() {
window.open( "openJcl" );
}

Tyler Christian
- 520
- 7
- 14
-
I'm finding that I can't manipulate DOM elements above the route that I am currently using with this method. – Tyler Christian Feb 28 '17 at 16:44
0
this.router.navigate([]).then(result => { window.open(`/example/test/${my_var}`, '_blank'); });
Angular 2 Routing navigate run in new tab(Use Angular Router naviagte )

drew0530
- 1
- 3