Home > Mobile >  Open the link generated on the button in a new tab
Open the link generated on the button in a new tab

Time:10-25

I have a button where it makes the API call and generate the URL , I want to open the URL in the new tab once I click the button.

<button (click)="getUrl()">Connect</button>

TS:

getUrl() {
    setTimeout(() => {
      this.url = 'www.google.com';
      console.log(this.url);
    }, 1000);
  }

what is the better way to get the URL from the API and open the link in the new tab. API may take more than 50 seconds to return the URL

stackblitz link to edit

CodePudding user response:

You can use window.open() with target _blank. If that answers your question:

window.open(this.url, '_blank');

See updated StackBlitz.

CodePudding user response:

use window.open(this.url, '_blank').focus(); like

getUrl() {
setTimeout(() => {
  window.open(this.url, '_blank').focus();
  this.url = 'www.google.com';
  console.log(this.url);
}, 1000);

} }

  • Related