How to open a link in a new tab in Angular 2.0?

I know that there is a $ window service in angular 1.X. Now I want to open the URL in a new tab, I did not find any service for this.

Update: it looks like Angular itself does not provide a method / service like ng1. If you want to navigate inside Angular, you can refer to this: Angular Router - navigate () . I ask this question because I want to navigate using some method provided by Angular, but none of the answers are expected.

+6
source share
3 answers

It seems like the OP wanted to know how to use $ window in angular2 to open a link in a new tab or window - so as not to distinguish between them. If this helps someone, first declare a window:

interface MyWindow extends Window {
    myFunction(): void;
}

declare var window: MyWindow;

then open the link as usual

window.open(url);
+8
source

You can set target = "_ blank" on your anchor tag to open the URL in a new tab.

<a [href]="url" target="_blank">Some URL</a>
+6
source

nagivationEnd HTML this [routerLink]="moduleMenu.ROUTE"

this.router.events.subscribe(event => {
        if (event instanceof NavigationEnd) {
            let subMenu: Menus = new Menus();
            var appName: string = localStorage.getItem('activeGroupModuleName');
            var menuItems: Menus[] = JSON.parse(localStorage.getItem('MenuList')) as Menus[];

            if (event.url != '/atpar') {
                if (menuItems != null && menuItems != undefined) {
                    for (var i = 0; i < menuItems.length; i++) {
                        if (event.url.indexOf(menuItems[i].ROUTE) >= 0 && appName == menuItems[i].APP_NAME) {
                            subMenu = menuItems[i];
                            break;
                        }
                    }

                    localStorage.setItem('submenu', JSON.stringify(subMenu));
                    this.title.setTitle(AtParConstants.PRODUCT_NAME + ' - ' + subMenu.MENU_NAME);
                }
                else {
                    this.title.setTitle(AtParConstants.PRODUCT_NAME + ' - ' + 'Dashboard');
                }
            }
            else {
                this.title.setTitle(AtParConstants.PRODUCT_NAME + ' - Dashboard');
            }
        }
    });
0

Source: https://habr.com/ru/post/1620257/


All Articles