我试图通过发送邮到PHP文件做一个简单的身份验证服务,我需要它装载在我的主页部分ng-view
时,其成功的。
这是我的尝试:
function loginCtrl($scope, $http, $location){
$http.post(url,data).success(function(data){
$location.path('/home');
});
}
结果在我的URL变化,但ng-view
不更新。 它更新,当我手动刷新页面。
(路线已在正确配置$routeProvider
,我已经测试了一个独立的功能,而不是作为一个回调重定向这和它的作品)
我也试图定义$location.path('/home')
作为一个函数,然后调用它仍然无法正常工作的回调。
我做了一些研究,发现了一些文章,指出这一点使用其他第三方插件的时候发生了,我只加载angular.js
任何见解或指点一下学习材料将是巨大的
以下是这篇文章的changeLocation例如http://www.yearofmoo.com/2012/10/more-angularjs-magic-to-supercharge-your-webapp.html#apply-digest-and-phase
//be sure to inject $scope and $location
var changeLocation = function(url, forceReload) {
$scope = $scope || angular.element(document).scope();
if(forceReload || $scope.$$phase) {
window.location = url;
}
else {
//only use this if you want to replace the history stack
//$location.path(url).replace();
//this this if you want to change the URL and add it to the history stack
$location.path(url);
$scope.$apply();
}
};
还有就是官方指南中简单的答案:
这是什么不能做?
当浏览器URL发生变化,也不会导致重新加载整个页面。 要更改URL后重新载入页面,使用较低级别的API,$ window.location.href。
来源: https://docs.angularjs.org/guide/$location
我做了下面的页面重定向(从登录到主页)。 我也必须通过用户对象主页。 所以,我使用Windows的localStorage。
$http({
url:'/login/user',
method : 'POST',
headers: {
'Content-Type': 'application/json'
},
data: userData
}).success(function(loginDetails){
$scope.updLoginDetails = loginDetails;
if($scope.updLoginDetails.successful == true)
{
loginDetails.custId = $scope.updLoginDetails.customerDetails.cust_ID;
loginDetails.userName = $scope.updLoginDetails.customerDetails.cust_NM;
window.localStorage.setItem("loginDetails", JSON.stringify(loginDetails));
$window.location='/login/homepage';
}
else
alert('No access available.');
}).error(function(err,status){
alert('No access available.');
});
它为我工作。
而不是使用的success
,我将其更改为then
和它的作品。
这里是代码:
lgrg.controller('login', function($scope, $window, $http) {
$scope.loginUser = {};
$scope.submitForm = function() {
$scope.errorInfo = null
$http({
method : 'POST',
url : '/login',
headers : {'Content-Type': 'application/json'}
data: $scope.loginUser
}).then(function(data) {
if (!data.status) {
$scope.errorInfo = data.info
} else {
//page jump
$window.location.href = '/admin';
}
});
};
});
使用方法:$ window.location.href = '/Home.html';
这是很容易的代码..但很难罚款..
detailsApp.controller("SchoolCtrl", function ($scope, $location) {
$scope.addSchool = function () {
location.href='/ManageSchool/TeacherProfile?ID=' + $scope.TeacherID;
}
});
文章来源: In Angular, how to redirect with $location.path as $http.post success callback