Haskell Fun
Haskell Fun

Reputation: 299

How to redirect to external URL in angular from entry menu

In my menu definiton I have something like that:

    ...
function routeConf($stateProvider) {
    $stateProvider.state({
        name: 'application.users',
        url: '/users' // in this place I would like to replace with http://google.com
    })
}

    ..

I would like to reach effect that on click this button it redirects to external URL, eg. http://google.com. I don't know Angular. Can you help me?
When I click a button it moves to /home/users. When I replace url : /dogs it moves to /home/dogs. As you can see, it appends suffix.

How to force not appending (in order to make it possible move to google)

Upvotes: 0

Views: 2128

Answers (1)

Radouane ROUFID
Radouane ROUFID

Reputation: 10813

In your view, bind the click event to a component method :

<button (click)="openUrl('www.google.com')">button</button>

And in your component use window.location.href to make the redirection:

openUrl(siteWeb: string) {

    let link = siteWeb;

    let httpPrefix = "http://";
    if (!siteWeb.startsWith(httpPrefix)) {
      link = httpPrefix + siteWeb
    }

    window.location.href = link;
  }

Note : Your link must be prefixed with http:// to make an external navigation.

Upvotes: 1

Related Questions