Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

javascript - 如何使用AngularJS重定向到另一个页面?(How to redirect to another page using 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.(我正在使用ajax调用来执行服务文件中的功能,如果响应成功,我想将页面重定向到另一个URL。)

Currently, I am doing this by using simple js "window.location = response['message'];".(目前,我通过使用简单的js“window.location = response ['message'];”来做到这一点。) But I need to replace it with angularjs code.(但我需要用angularjs代码替换它。) I have looked various solutions on stackoverflow, they used $location.(我在stackoverflow上看了各种解决方案,他们使用了$ 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 })   ask by Farjad Hasan translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use Angular $window :(您可以使用Angular $window :)

$window.location.href = '/index.html'; Example usage in a contoller:(控制器中的示例用法:) (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'); }); } } })();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...