Angular 2 - Redirect to an external URL and open in a new tab

前端 未结 9 547
离开以前
离开以前 2020-12-03 00:36

I\'m trying to Open a new page when user clicks on a link. I can\'t use Angular 2 Router, because it doesn\'t have any functionalities to redirect to an external URL.

<
相关标签:
9条回答
  • 2020-12-03 01:02

    in HTML:

    <a class="main-item" (click)="onNavigate()">Go to URL</a>
    

    OR

    <button class="main-item" (click)="onNavigate()">Go to URL</button>
    

    in TS file:

    onNavigate(){
        // your logic here.... like set the url 
        const url = 'https://www.google.com';
        window.open(url, '_blank');
    }
    
    0 讨论(0)
  • 2020-12-03 01:09

    Another possible solution is:

    const link = document.createElement('a');
    link.target = '_blank';
    link.href = 'https://www.google.es';
    link.setAttribute('visibility', 'hidden');
    link.click();
    
    0 讨论(0)
  • 2020-12-03 01:12

    you can do like this in your typescript code

    onNavigate(){
    var location="https://google.com",
    }
    

    In your html code add an anchor tag and pass that variable(location)

    <a href="{{location}}" target="_blank">Redirect Location</a>
    

    Suppose you have url like this www.google.com without http and you are not getting redirected to the given url then add http:// to the location like this

    var location = 'http://'+ www.google.com
    
    0 讨论(0)
提交回复
热议问题