Redirect to a different page after POST using AngularJS

Issue

After searching and not coming up with a solution I am posting this code snippet for some help.

$scope.createAddress = function () {
    $http({
        method: 'POST',
        url: '/addressBook/api/Person',
        data: $scope.person,
        headers: {
            'Content-Type': 'application/json; charset=utf-8'
        }
    }).success(function (data) {
        $location.path('/addressBook');
    });
}

After successfully posting I would like to redirect to a different page. $location.path is not accomplishing this. I have tried $scope.apply() as some others have had success with that. Am I missing something or not understanding what $location is used for? The code is being hit.

Solution

There should be something listening on that path change, like a $routeProvider. Is that the case?

If you need a full page reload to that other (server-side) route you might try $window.location.href.

Answered By – Jair Trejo

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published