javascript - 重定向到 Angular JS 中的另一个 HTML 页面

标签 javascript html angularjs angularjs-routing

我编写了一个没有实际服务器的简单登录表单。只是一个硬编码的登录凭据。如果他们的凭据在 Angular Controller 功能上匹配,那么我需要显示另一个 HTML 页面。在'/login.html'上,写了下面的代码

<!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>

在“login.js”中,代码​​如下:

(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')
            }
        }

    }]);

})();

但是在 $location.href('/afterLogin.html'); 上,它什么都不做。即使没有错误。可能是什么原因?

最佳答案

你能不能像下面这样试试:

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

关于javascript - 重定向到 Angular JS 中的另一个 HTML 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29362600/

相关文章:

angularjs - 了解 AngularJS ng-src

javascript - 试图让大写小写 jquery 代码工作

javascript - 将数字添加到数组元素时发生了什么

javascript - 如何使 Microsoft Internet Explorer 表现得像一个符合标准的浏览器?

html - 缩略图图片断早了

javascript - AngularJS 指令中 CSS 的事件监听器未被触发/捕获

javascript - 如何测试 JQuery 失败回调?

javascript - Chrome 44 不再支持以下语法 : window. location.href = "javascript:someFunction()"

javascript - 微软风格的标签

html - 如何在同一列中的两个 DIV 之间留出空白