13

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.

6 Answers 6

22

Remember to inject the dependency to the controller:

                                                       |-- inject!
angular.module('myApp.controllers', []).               v
controller('AddressController', function ($scope, $location, $http) {
  $http({
    method: 'POST',
    url: '/addressBook/api/Person',
    data: $scope.person,
    headers: {
        'Content-Type': 'application/json; charset=utf-8'
    }
  }).success(function (data) {
    $location.path('/addressBook');
  });
});
2
  • $window.location.href works, but your option - doesnt (but seems better), what can i miss ? Dec 27, 2014 at 16:16
  • Thanks for the answer it helped me in redirecting. Keep helping. Aug 22, 2016 at 12:26
10

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.

1
  • 1
    $window.location.href = '/addressbook'; and then $window.location.href; worked. Thanks for the help! Sep 3, 2013 at 19:37
1

in your controller add the $location. like this Ex: app.controller('yourController', function($scope, $location, $http, etc...) then on your $http should be like this.

$http({.... }).success(function(response) { $location.path('/your path')})

but make sure that you must add additional location to your $routeProvider Configuration. something like:

app.config(['$routeProvider', 
   function($routeProvider) { 
   $routeProvider.
     when('/yournewlocation', {
      templateUrl: 'yourTemplate/yourpage'
   })
  }
])

thats it. well you know what im talking about.

0
.controller('regCtrl', ['$scope','$http', function($scope,$http) {

        $scope.submit = function() {
            alert("vf");

            $http.post("http://localhost:3000/insert",function(response,$location){
                if(response!= null){
                $location.path("/login");
            }
            else{
                $state.go('/register');
            }
            });
            }

            }]);

my code is like this i am getting only json response after register page its not navigate to another page

0

simply use window.location.replace("/Home/Index");

1
  • 2
    Please consider explaining how and why this solves the poblem.
    – vlizana
    Jul 11, 2020 at 8:22
-1

the best way and lightest is

window.location.replace("/home/list");

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.