Link to home
Start Free TrialLog in
Avatar of ank5
ank5Flag for India

asked on

Angular 4 - open pop up window

If I do the below, it opens the window in a new tab

<a [routerLink]="['/name', 'param1']" target="_blank" >{{ name }}</a>

Open in new window


Instead of a new tab, I need to open the same route in a pop up window. Javascript code to open a pop up window looks like this

 <a href="javascript:void(0);" onclick="window.open(url, '_blank', 'width=300,height=250');">test</a>

Open in new window


I am not able to make out how to fit in routerLink in this example. Is there another way to open pop up window when using angular?
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Define your link like this
<a href="" (click)="newWindow(url)">New window</a>

Open in new window

In your component
newWindow(url) {
        window.open(url, '_blank', 'width=300,height=250');
}

Open in new window


EDIT:
The above assumes that the url variable has been defined as in scope.
Avatar of ank5

ASKER

How do I load the router and pass parameters to it? Code which opens new tab uses a RouterLink

<a [routerLink]="['/name', 'param1']" target="_blank" >{{ name }}</a>

Open in new window


I am using angular 4
Why do you want to use the router - you are not routing anywhere  - you are opening a new window - that falls outside of the router.
Avatar of ank5

ASKER

Sorry, didn't quite get it. How would I tell Angular which component to load if I do not use Routing?

In app-routing.module.ts, I have defined which Component to load -

 
    { path: 'name/:id', component: NameComponent},

Open in new window


Now, in the Javascript function, I would need to do something like

 newWindow(id: string) {
    let url = "http://localhost:4200/#/name/"+id; //somehow get this host name dynamically, but that's for later
    console.log(url);
    window.open(url, '_blank', 'width=300,height=250');
  }

Open in new window


and the html link as

<a href="" (click)="newWindow(selectedID)"  >{{ name }}</a>

Open in new window


If I do this, I am still using Routing to tell Angular which Component to load, so I didn't quite get how to not use Routing in this scenario. Is there another way to load components?

If I use the above approach, I main page routes to the default Route once the link is clicked.
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ank5

ASKER

Thanks! Got it.

So, what is incorrect in my implementation that I mentioned in my previous post? what do I need to do in order to load the component without specifying it in app-routing.module.ts?
Does the path not work as per my sample?

Simply put the path to the module with the parameters in the URL and open the window?