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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29362600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:25:12  来源:igfitidea点击:

Redirect to another HTML page in Angular JS

javascripthtmlangularjsangularjs-routing

提问by user3853055

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

我编写了一个没有实际服务器的简单登录表单。只是一个硬编码的登录凭据。如果他们的凭据在角度控制器功能上匹配,那么我需要显示另一个 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>

And in 'login.js' the code is following :

在“ 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')
            }
        }

    }]);

})();

But on $location.href('/afterLogin.html');, it does nothing. Even there is no error. What could be the cause?

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

回答by sms

Can you try like this below:

你可以像下面这样尝试:

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

回答by Jairo Malanay

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

您可以使用有角度的$window。就像短信在之前的重定向答案中所说的一样。

Another way is through Routing - ngRouteor ui-router

另一种方式是通过路由 - ngRouteui-router

回答by Reds

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

这可能对你有帮助。 使用 $location 重定向到 AngularJS 中的新页面

Giving proper path will help you I think.