Post 使用AngularJS发布后重定向到其他页面
经过搜索,并没有找到一个解决方案,我张贴了一些帮助的代码片段Post 使用AngularJS发布后重定向到其他页面,post,redirect,angularjs,location,Post,Redirect,Angularjs,Location,经过搜索,并没有找到一个解决方案,我张贴了一些帮助的代码片段 $scope.createAddress = function () { $http({ method: 'POST', url: '/addressBook/api/Person', data: $scope.person, headers: { 'Content-Type': 'application/json; charset=utf
$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');
});
}
成功发布后,我想重定向到其他页面$location.path无法完成此操作。我尝试了$scope.apply(),其他一些人在这方面也取得了成功。我是否遗漏了什么或不理解$location的用途?代码正在被命中。应该有一些东西在侦听路径更改,例如
$routeProvider
。是这样吗
如果需要将整个页面重新加载到该其他(服务器端)路由,您可以尝试
$window.location.href
记住将依赖项注入控制器:
|-- 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');
});
});
在控制器中添加$location。这样地 例如:app.controller('yourController',function($scope、$location、$http等) 那么在您的$http上应该是这样的
$http({.... }).success(function(response) { $location.path('/your path')})
但请确保必须向$routeProvider配置中添加其他位置。
比如:
app.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/yournewlocation', {
templateUrl: 'yourTemplate/yourpage'
})
}
])
就是这样,你知道我在说什么
.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');
}
});
}
}]);
我的代码是这样的,在注册页面之后,我只得到json响应,而不是导航到另一个页面只需使用
window.location.replace(“/Home/Index”);
最好的方法是
window.location.replace(“/home/list”);
$window.location.href='/addressbook';
,然后$window.location.href;
工作。感谢您的帮助!$window.location.href工作正常,但您的选项-没有(但似乎更好)我能错过什么?谢谢你的回答,它帮助我重定向。继续帮助。请考虑解释如何以及为什么它解决了问题。同样的解决方案,其他答案,如没有看到,冷静!