0

I have a task to implement opening a url in a different tab when clicking on an item in a dropdown menu using Angular 4.4.6 I have a hack solution using a dummy component with a window.open() method to get the job done. But I tend to think there is a more elegant solution to this. Tried a hack using a derivative of this answer, which I believe is close, but I'm missing something. Here is a snippet of what I am trying:

app.module.ts provider excerpt of Resolver

    {
        provide: 'urlRedirectResolver', useValue: (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => {
            //Something like this
            window.open((route.data as any).otherUrl);
            //OR
            window.location.href = window.open((route.data as any).otherUrl); 
        }
    }

app.router.ts route excerpt of Implementation

    path: 'SomeComponentURL', component: SomeComponent, resolve: {
        url: 'urlRedirectResolver'
    },
    data: {
        otherUrl: (document.location.hash != '#/SomeComponent')
            ? 'someContent.pdf' : history.back()
    }

If the question is too vague please let me know and I'll try to revise it. Any help is greatly appreciated!

BSchnitzel
  • 136
  • 2
  • 15
  • That actually works for other requirements in the app, but the dropdown list is generated dynamically :-) I'm trying to solve this without creating another component and using only a resolver – BSchnitzel Mar 02 '18 at 19:03
  • I don't see how the fact that it's generated dynamically prevents using target="_blank". A resolve is used to load data needed by a routed component. Not to open a link in a new tab. – JB Nizet Mar 02 '18 at 19:06
  • Hmm, are you referring to something like window.open((route.data as any).otherUrl, '_blank') cause I tried that already and it didn't work. It just made the page blink – BSchnitzel Mar 02 '18 at 19:10
  • 1
    No. I'm telling you that you're using a resolve for something it's not supposed to be used for. Instead, you should use target="_blank" on your links. – JB Nizet Mar 02 '18 at 19:11
  • Not all links do the same thing. I get what you are saying, I'm just posting a question and looking for feedback thanks – BSchnitzel Mar 02 '18 at 19:13
  • 1
    Then set the target to _blank on links that must open in a new tab, and set it to _self for links that must not. – JB Nizet Mar 02 '18 at 19:14

0 Answers0