使用AngularJS在POST后重定向到不同的页面

Den*_*sel 13 post redirect location angularjs

在搜索并没有提出解决方案后,我发布此代码片段以获得一些帮助.

$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');
    });
}
Run Code Online (Sandbox Code Playgroud)

成功发布后,我想重定向到另一个页面.$ location.path没有实现这一点.我尝试了$ scope.apply(),因为其他一些人已经取得了成功.我错过了什么或不了解$ location用于什么?代码被击中了.

Sgn*_*gnl 22

记得将依赖项注入控制器:

                                                       |-- 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');
  });
});
Run Code Online (Sandbox Code Playgroud)


Jai*_*ejo 10

应该听一下这条路径的变化,比如说$routeProvider.是这样的吗?

如果您需要将整页重新加载到其他(服务器端)路由,您可以尝试$window.location.href.