从angularjs重定向登录页面到欢迎页面
redirect login page to welcome page from angularjs
我正在创建一个web应用程序,我需要从登录重定向到欢迎页面,如果我的用户id和密码验证
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function ($scope, $http, $interval) {
//login
$scope.loginbutton = function () {
$http.get('/login.asmx/loginuser', {
params: {
log: $scope.log,
pm: $scope.pm,
password: $scope.password
}
})
.then(function (response) {
{
//here i want to redirect to another page
}
})
}
});
</script>
这是我的脚本的angularjs我如何重定向页面到另一个页面?
可以使用$location服务,例如:
<br>$location.path('/home')<br>
这里是refguid链接获取更多信息
在控制器中添加$window
app.controller('customersCtrl', function ($scope, $http, $interval, $location, $window) {
然后在您的回复
中添加以下行.then(function (response) {
{
$window.location.href = "../welcome/Index";
}
})
<script>
var app = angular.module('myApp', ['$location']);
app.controller('customersCtrl', function ($scope, $http, $interval, $location) { // add $location dependency in the
//login
$scope.loginbutton = function () {
$http.get('/login.asmx/loginuser', {
params: {
log: $scope.log,
pm: $scope.pm,
password: $scope.password
}
})
.then(function (response) {
{
$location.path('google.com') //pass the url in it.
}
})
}
});
</script>
使用$state
重定向
controller.js
controller('customersCtrl', function ($scope, $http, $interval, $state) {
$scope.loginbutton = function () {
$http.get('/login.asmx/loginuser', {
params: {
log: $scope.log,
pm: $scope.pm,
password: $scope.password
}
})
.then(function (response) {
$state.go('page');
})
}
});
然后在你的route.js中指定要重定向到的新状态。
angular.module('app.routes', []);
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('page', {
url: '/page',
templateUrl: 'templates/page.html',
controller: 'pageCtrl'
})
});
您需要在控制器中包含$location
依赖项,并且需要调用$location.url('/login')
来导航到登录页面。我已经为此修改了你的代码。
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function ($scope, $http, $interval, $location) {
//login
$scope.loginbutton = function () {
$http.get('/login.asmx/loginuser', {
params: {
log: $scope.log,
pm: $scope.pm,
password: $scope.password
}
})
.then(function (response) {
{
//Navigate to welcome page
$location.url('/welcome');
}
})
}
});
</script>
你需要在你的路由提供程序中定义一个welcome
路径,如:
(function () {
'use strict';
angular.module('myApp', [
'ngRoute'
])
.config(['$routeProvider', function ($routeProvider) {
$routeProvider.when('/login', {
templateUrl: 'views/landing.html',
controller: 'landingController'
});
$routeProvider.when('/customers', {
templateUrl: 'views/customers.html',
controller: 'customersCtrl'
});
$routeProvider.when('/welcome', {
templateUrl: 'views/welcome.html',
controller: 'welcomeController'
});
..............
..............
$routeProvider.otherwise({
redirectTo: '/login'
});
}]);
})();
相关文章:
- 如何检查用户是否使用$routeChangeStart登录angularjs
- AngularJS+PHP.登录面板
- 如果需要登录,Angularjs$http拦截器会暂停异步请求
- 使用AngularJS登录页面
- AngularJS中的登录页面
- AngularJS:转发到登录页面进行身份验证,保留路由作为GET参数
- Angularjs Facebook登录:无法读取属性'appendChild'为null
- AngularJS SPA 登录页面
- 使用 angularjs 登录
- 希望仅为已登录的用户显示链接,否则它显示该链接在AngularJS和NodeJS中被隐藏或禁用
- 想要在登录页面和其他页面应用程序 angularjs 之间分开
- 使用angularjs,登录页面和维护“会话”的安全性
- 使用 AngularJS 登录 Spring Boot $http.post
- AngularJS登录后重定向
- Angularjs 登录 cookie 第二次工作
- 确保用户使用cookieStore和AngularJS登录或注销的最佳实践
- Google OAuth2 Devise使用AngularJS登录
- 使用angularjs登录时的Ionic认证
- 使用 rest api 和 angularjs 登录 asp.net
- Angularjs 登录身份验证重定向获取 -- 错误:发送标头后无法设置标头