user3853055
user3853055

Reputation: 227

Redirect to another HTML page in Angular JS

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>Welcome 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?

Upvotes: 1

Views: 13220

Answers (3)

Reds
Reds

Reputation: 1

This might help you. Redirect to new Page in AngularJS using $location

Giving proper path will help you I think.

Upvotes: 0

Jairo Malanay
Jairo Malanay

Reputation: 1347

you can use the angular $window. just like sms said in previous answer for redirection.

Another way is through Routing - ngRoute or ui-router

Upvotes: 1

sms
sms

Reputation: 426

Can you try like this below:

 var path = "/afterLogin.html";
 window.location.href = path;

Upvotes: 3

Related Questions