如何使用AngularJS重定向到另一个页面? [英] How to redirect to another page using AngularJS?
本文介绍了如何使用AngularJS重定向到另一个页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用ajax调用来执行服务文件中的功能,如果响应成功,我想将页面重定向到另一个URL。目前,我通过使用简单的jswindow.location = response ['message'];来做到这一点。但我需要用angularjs代码替换它。我在stackoverflow上看了各种解决方案,他们使用了$ location。但我是角色新手并且无法实现它。
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 using simple js "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 angular 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屋!
查看全文