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?

Share Improve this question asked Mar 31, 2015 at 7:05 user3853055user3853055 2271 gold badge3 silver badges17 bronze badges 6
  • 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
 |  Show 1 more ment

3 Answers 3

Reset to default 3

Can 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