Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Routing run in new tab

I am working with a asp.net core application with angular2 and my routing is working fine.

<a target="target" routerLink="/page1" routerLinkActive="active">Associates</a>  <a routerLink="/page2" routerLinkActive="active">Account managers</a>  

I want to open every page link (routerLink) in a new tab. Is it possible that every link is open in a new tab, without refreshing the page?

I have tried to replace routerLink="/Page2" by target="target" href="/associates" but the page refreshes all the reference.

like image 778
coder Avatar asked Dec 28 '16 05:12

coder


People also ask

How do I make my router open in a new tab?

Left click -> go to corresponding route / page without a new reload ; Right click -> Open in new tab / window options and would naturally load a new page when using this navigation.

How to redirect Angular?

To set up a redirect, configure a route with the path you want to redirect from, the component you want to redirect to, and a pathMatch value that tells the router how to match the URL.

What is the command for routing in Angular?

RouterModule.forRoot() link The method is called forRoot() because you configure the router at the application's root level. The forRoot() method supplies the service providers and directives needed for routing, and performs the initial navigation based on the current browser URL.

How to navigate Ionic?

Navigation in Ionic works like a simple stack, where we push new pages onto the top of the stack, which takes us forwards in the app and shows a back button. To go backwards, we pop the top page off.


1 Answers

Try this please, <a target="_blank" routerLink="/Page2">


Update1: Custom directives to the rescue! Full code is here: https://github.com/pokearound/angular2-olnw

import { Directive, ElementRef, HostListener, Input, Inject } from '@angular/core';  @Directive({ selector: '[olinw007]' }) export class OpenLinkInNewWindowDirective {     //@Input('olinwLink') link: string; //intro a new attribute, if independent from routerLink     @Input('routerLink') link: string;     constructor(private el: ElementRef, @Inject(Window) private win:Window) {     }     @HostListener('mousedown') onMouseEnter() {         this.win.open(this.link || 'main/default');     } } 

Notice, Window is provided and OpenLinkInNewWindowDirective declared below:

import { AppAboutComponent } from './app.about.component'; import { AppDefaultComponent } from './app.default.component'; import { PageNotFoundComponent } from './app.pnf.component'; import { OpenLinkInNewWindowDirective } from './olinw.directive'; import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { HttpModule } from '@angular/http'; import { RouterModule, Routes } from '@angular/router';  import { AppComponent } from './app.component';   const appRoutes: Routes = [   { path: '', pathMatch: 'full', component: AppDefaultComponent },   { path: 'home', component: AppComponent },   { path: 'about', component: AppAboutComponent },   { path: '**', component: PageNotFoundComponent } ];  @NgModule({   declarations: [     AppComponent, AppAboutComponent, AppDefaultComponent, PageNotFoundComponent, OpenLinkInNewWindowDirective   ],   imports: [     BrowserModule,     FormsModule,     HttpModule,     RouterModule.forRoot(appRoutes)   ],   providers: [{ provide: Window, useValue: window }],   bootstrap: [AppComponent] }) export class AppModule { } 

First link opens in new Window, second one will not:

<h1>     {{title}}     <ul>         <li><a routerLink="/main/home" routerLinkActive="active" olinw007> OLNW</a></li>         <li><a routerLink="/main/home" routerLinkActive="active"> OLNW - NOT</a></li>     </ul>     <div style="background-color:#eee;">         <router-outlet></router-outlet>     </div> </h1> 

Tada! ..and you are welcome =)

Update2: As of v2.4.10 <a target="_blank" routerLink="/Page2"> works

like image 159
Anand Rockzz Avatar answered Oct 12 '22 03:10

Anand Rockzz