shapiro shapiro - 1 month ago 13
AngularJS Question

location.path() not redirecting to page

In node this is how I define my details route (render as jade and send).

app.get('/details', function(req, res){

jade.renderFile('details.jade', function(err, html){
if(err){
console.log(err);
}
res.send(html);
});

});


In jade with 'blah' is clicked then calls navigateToPath function with params.

a(ng-click="navigateToPath(date_obj_key, part)") blah


In angular, this function should go to this path. The url path changes in the browser but it does not REDIRECT to the page. Help (yes I am injecting the location service)

$scope.navigateToPath = function(date, part){
var path = '/details?date='+date+'&part_type='+part;
$location.path('/details').replace(); //also tried $location.url(path)
$scope.apply(); //also tried $scope.$apply and $rootScope.$apply as func
console.log($location.path());
}

Answer

I am using Fire Fox developer tools(F12) and put a break point on where I used $window.location in my project and looked at the values in $window.location and this what it shows:

enter image description here

It seems like this would work. for both a location in the same folder or sub-folder as well as going to a completely different web site.

 $window.location = $window.location.origin + path

or

 $window.location = <whatever website you want to go to>

In my case I was just using the $window.location to call a rest service to download a file that the user selected from a ui-grid while still staying on the same page. and this may have worked for me because my scenario is a bit different then what yours is I think. so all I had to do was

 $window.location = "../../services" + "<path to my rest service>" + $scope.shortCode + "/" + $scope.wireInstSelectedRow.entity.fileName;

@shapiro I am not sure why this does not work

 $location.path('/details').replace();

I tried the same thing originally in my project and based on the documentation: https://docs.angularjs.org/api/ng/service/$location It seems like that would work and what its supposed to be used for but what I noticed is it would put a '#' character in the url before it put the path I wanted it to take which was preventing it from going to that page. Anyhow for me it seems like as long as you are going to an html page that is in the same folder or sub-folder just doing

 $window.location = <the path you want to go to>;

is a good solution... at least it did the trick for me. Hope this helps.