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


        };
Gilberto Quintero
  • 397
  • 1
  • 6
  • 18
  • Try `$scope.searchPage = function (url) { return www.mypage.com + url};` – Hassan Imam Jul 13 '17 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 – Gilberto Quintero Jul 13 '17 at 17:23
  • 1
    [$location](https://docs.angularjs.org/guide/$location), [How to redirect to another page using AngularJS](https://stackoverflow.com/questions/27941876/how-to-redirect-to-another-page-using-angular-js), [How to redirect to another webpage in JavaScript/jQuery?](https://stackoverflow.com/questions/503093/how-to-redirect-to-another-webpage-in-javascript-jquery) – Patrick Barr Jul 13 '17 at 17:23
  • use $location.path(url) and see this too window.location.hostname – Mohsen Jul 13 '17 at 17:24
  • Use `$location.path(url)` or `window.location.href`. – Hassan Imam Jul 13 '17 at 17:25

2 Answers2

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');
codtex
  • 6,128
  • 2
  • 17
  • 34
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.

samanime
  • 25,408
  • 15
  • 90
  • 139