0

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


        };
5
  • Try $scope.searchPage = function (url) { return www.mypage.com + url}; Commented Jul 13, 2017 at 17:22
  • 1
    no page is not supposed to be hardcoded i want to get it with a js funcion I heard there is something like $location Commented Jul 13, 2017 at 17:23
  • 1
    $location, How to redirect to another page using AngularJS, How to redirect to another webpage in JavaScript/jQuery? Commented Jul 13, 2017 at 17:23
  • use $location.path(url) and see this too window.location.hostname Commented Jul 13, 2017 at 17:24
  • Use $location.path(url) or window.location.href. Commented Jul 13, 2017 at 17:25

2 Answers 2

1

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');
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.