如何使用 AngularJS 重定向到另一个页面? [英] How to redirect to another page using AngularJS?
本文介绍了如何使用 AngularJS 重定向到另一个页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 ajax 调用在服务文件中执行功能,如果响应成功,我想将页面重定向到另一个 url.目前,我通过普通的 JS 代码 window.location = response['message'];
来做到这一点.但我需要用 AngularJS 代码替换它.我在 stackoverflow 上查看了各种解决方案,他们使用了 $location
.但我是 AngularJS 的新手,在实现它时遇到了麻烦.
I am using ajax call to perform functionality in a service file and if the response is successful, I want to redirect the page to another url. Currently, I am doing this by plain JS code window.location = response['message'];
. But I need to replace it with AngularJS code. I have looked various solutions on stackoverflow, they used $location
. But I am new to AngularJS and having trouble to implement it.
$http({
url: RootURL+'app-code/common.service.php',
method: "POST",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
dataType: 'json',
data:data + '&method=signin'
}).success(function (response) {
console.log(response);
if (response['code'] == '420') {
$scope.message = response['message'];
$scope.loginPassword = '';
}
else if (response['code'] != '200'){
$scope.message = response['message'];
$scope.loginPassword = '';
}
else {
window.location = response['message'];
}
// $scope.users = data.users; // assign $scope.persons here as promise is resolved here
})
推荐答案
你可以使用 Angular $window
:
You can use Angular $window
:
$window.location.href = '/index.html';
控制器中的示例用法:
(function () {
'use strict';
angular
.module('app')
.controller('LoginCtrl', LoginCtrl);
LoginCtrl.$inject = ['$window', 'loginSrv', 'notify'];
function LoginCtrl($window, loginSrv, notify) {
/* jshint validthis:true */
var vm = this;
vm.validateUser = function () {
loginSrv.validateLogin(vm.username, vm.password).then(function (data) {
if (data.isValidUser) {
$window.location.href = '/index.html';
}
else
alert('Login incorrect');
});
}
}
})();
这篇关于如何使用 AngularJS 重定向到另一个页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文