admin管理员组文章数量:1399754
I've written a simple Login form with no actual server. Just a hard coded login credentials. And if they credentials are matching on angular controller function then I need to show another HTML page. On '/login.html', following code is written
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="font-awesome/css/font-awesome.css" rel="stylesheet">
<link href="css/animate.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<script type="text/javascript" src="/InfoGraph/js/angular/angular.min.js" ></script>
<script type="text/javascript" src="/InfoGraph/js/angular/angular-route.min.js" ></script>
<script type="text/javascript" src="/InfoGraph/js/login.js" ></script>
</head>
<body class="gray-bg" ng-app="login">
<div class="middle-box text-center loginscreen animated fadeInDown">
<div>
<div>
<h1 class="logo-name">IGraph</h1>
</div>
<h3>Wele to IGraph</h3>
<form class="m-t" role="form" name="loginToIGraph" ng-controller="LoginController as login">
<div class="form-group">
<input type="text" class="form-control" placeholder="Username" required="" ng-model="user.username" >
</div>
<div class="form-group">
<input type="password" class="form-control" placeholder="Password" required="" ng-model="user.password" >
</div>
<button type="submit" class="btn btn-primary block full-width m-b" ng-click="login.loginUser(user)">Login</button>
<a href="#"><small>Forgot password?</small></a>
<p class="text-muted text-center"><small>Do not have an account?</small></p>
<a class="btn btn-sm btn-white btn-block" href="register.html">Create an account</a>
</form>
</div>
</div>
</body>
</html>
And in 'login.js
' the code is following :
(function(){
var app = angular.module('login',[]);
app.controller('LoginController', ['$scope','$location', function($scope,$location)
{
$scope.loginUser = function(user){
if(user.username == 'demo' && user.password == 'demo'){
$location.href('/afterLogin.html');
}
else{
alert('Wrong credentials')
}
}
}]);
})();
But on $location.href('/afterLogin.html');
, it does nothing. Even there is no error. What could be the cause?
I've written a simple Login form with no actual server. Just a hard coded login credentials. And if they credentials are matching on angular controller function then I need to show another HTML page. On '/login.html', following code is written
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="font-awesome/css/font-awesome.css" rel="stylesheet">
<link href="css/animate.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<script type="text/javascript" src="/InfoGraph/js/angular/angular.min.js" ></script>
<script type="text/javascript" src="/InfoGraph/js/angular/angular-route.min.js" ></script>
<script type="text/javascript" src="/InfoGraph/js/login.js" ></script>
</head>
<body class="gray-bg" ng-app="login">
<div class="middle-box text-center loginscreen animated fadeInDown">
<div>
<div>
<h1 class="logo-name">IGraph</h1>
</div>
<h3>Wele to IGraph</h3>
<form class="m-t" role="form" name="loginToIGraph" ng-controller="LoginController as login">
<div class="form-group">
<input type="text" class="form-control" placeholder="Username" required="" ng-model="user.username" >
</div>
<div class="form-group">
<input type="password" class="form-control" placeholder="Password" required="" ng-model="user.password" >
</div>
<button type="submit" class="btn btn-primary block full-width m-b" ng-click="login.loginUser(user)">Login</button>
<a href="#"><small>Forgot password?</small></a>
<p class="text-muted text-center"><small>Do not have an account?</small></p>
<a class="btn btn-sm btn-white btn-block" href="register.html">Create an account</a>
</form>
</div>
</div>
</body>
</html>
And in 'login.js
' the code is following :
(function(){
var app = angular.module('login',[]);
app.controller('LoginController', ['$scope','$location', function($scope,$location)
{
$scope.loginUser = function(user){
if(user.username == 'demo' && user.password == 'demo'){
$location.href('/afterLogin.html');
}
else{
alert('Wrong credentials')
}
}
}]);
})();
But on $location.href('/afterLogin.html');
, it does nothing. Even there is no error. What could be the cause?
- don't use $location, use window.location – Michael Kang Commented Mar 31, 2015 at 7:08
-
does
$location.path(url)
work ? – Yasser Shaikh Commented Mar 31, 2015 at 7:09 - 1 you should use $window.location.href for a redirection and if you check the $location documentation : docs.angularjs/api/ng/service/$location, you could see that href does not exist, then use $location.path(...) but i think here you want $window.location.href – Cedric Dumont Commented Mar 31, 2015 at 7:11
- I would suggest you to use routes / ng-view. That would be a more "Angular way" to achieve that. – enguerranws Commented Mar 31, 2015 at 7:34
- I've tried this. But there is nothing. No error and no success. the link does nothing if my credentials are correct. – user3853055 Commented Mar 31, 2015 at 9:10
3 Answers
Reset to default 3Can you try like this below:
var path = "/afterLogin.html";
window.location.href = path;
you can use the angular $window. just like sms said in previous answer for redirection.
Another way is through Routing - ngRoute or ui-router
This might help you. Redirect to new Page in AngularJS using $location
Giving proper path will help you I think.
本文标签: javascriptRedirect to another HTML page in Angular JSStack Overflow
版权声明:本文标题:javascript - Redirect to another HTML page in Angular JS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744212596a2595491.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论