Gilberto Quintero
Gilberto Quintero

Reputation: 397

How to change url using angular or javascript

I am trying to create a function that change to another page depending certain value, I just can't find a way to do it.

Something like this

get the current page www.mypage.com

insert a new route

url = "/reports"

so when the funcion executes the page will redirect to www.mypage.com/reports or anything depending the url

$scope.searchPage = function (url) {

/// page = www.mypage.com + url


        };

Upvotes: 0

Views: 2282

Answers (2)

codtex
codtex

Reputation: 6548

You can use $location(docs) to do this and your code will look like:

$location.path('/reports');

this will navigate to yourdomain.com/reports.

Another solution would be to use $window(docs) like:

$window.location.href = 'http://www.mypage.com/reports'

I personally use $location service to navigate inside the angularjs application and if you want to redirect to an external page in new tab via javascript you can do something like:

window.open('http://www.mypage.com/reports', '_blank');

Upvotes: 1

samanime
samanime

Reputation: 26527

To change the URL of a page with JavaScript, you just specify it on document.location.href:

document.location.href = 'http://example.com/new-page';

It is just a string, so you can build it however you want and pass it in.

For Angular, you can use the $window.location.href instead. Just be sure to inject the $window dependency:

$window.location.href = 'http://example.com/new-page';

Using the Angular way will allow a single-page-app to remain single page if it's within your site.

Upvotes: 0

Related Questions