如何使用AngularJs将一个页面导航到另一个页面

时间:2022-06-06 10:16:47

How to navigate from one page to another page. Assume i have a login page, after entering username and password fileds while click on submit button it needs to validate and if both username and password is correct it needs to go home page. Home page content needs to display. Here i am posting code. In Browser url is changing but content is not changing.

如何从一个页面导航到另一个页面。假设我有一个登录页面,在输入用户名和密码fileds后点击提交按钮需要验证,如果用户名和密码都正确,则需要进入主页。主页内容需要显示。我在这里发布代码。在浏览器中,网址正在发生变化,但内容未发生变化。

index.html

<html ng-app='myApp'>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"/>
<script
    src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>
<script
    src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular-route.js"></script>
</head>

<body >
    <div id='content' ng-controller='LoginController'>
        <div class="container">
            <form class="form-signin" role="form" ng-submit="login()">
                <h3 class="form-signin-heading">Login Form</h3>
                <span><b>Username :</b>&nbsp; <input type="username"
                    class="form-control" ng-model="user.name" required> </span> </br>
                </br> <span><b>Password :</b>&nbsp;&nbsp; <input type="password"
                    class="form-control" ng-model="user.password" required> </span> 
                <br><br>                
                <button class="btn btn-lg btn-primary btn-block" type="submit">
                    <b>Sign in</b>
                </button>
            </form>
        </div>
        <!-- /container -->
    </div>
    <script src='test.js' type="text/javascript"></script>
</body>
</html>

home.html

Hello I am  from Home page

test.js

var applog = angular.module('myApp',['ngRoute']);

applog.config([ '$routeProvider', '$locationProvider',
    function($routeProvider, $locationProvider) {   
        $routeProvider.when('/home', {
            templateUrl : 'home.html',
            controller : 'HomeController'
        }).otherwise({
            redirectTo : 'index.html'
        });
        //$locationProvider.html5Mode(true); //Remove the '#' from URL.
    } 
]);

applog.controller("LoginController", function($scope, $location) {
    $scope.login = function() {
        var username = $scope.user.name;
        var password = $scope.user.password;
        if (username == "admin" && password == "admin") {
            $location.path("/home" );
        } else {
            alert('invalid username and password');
        }
    };
});

applog.controller("HomeController", function($scope, $location) {

});

2 个解决方案

#1


5  

As @dfsq said you need an ng-view for placing there your new view. Of course if you add your ng-view in the index you will see the content of the index and of home. The solution here is creating two partial views, one for the log in authentication and the other for home. The index file should contain only the ng-view and those elements you want to keep constant in your whole app (menu, footer...)

正如@dfsq所说,你需要一个ng-view来放置你的新视图。当然,如果您在索引中添加ng-view,您将看到索引和home的内容。这里的解决方案是创建两个部分视图,一个用于登录身份验证,另一个用于主页。索引文件应仅包含ng-view以及要在整个应用程序中保持不变的元素(菜单,页脚...)

Here is the code for what I'm saying: index.html

这是我所说的代码:index.html

<html ng-app='myApp'>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"/>
<script
    src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>
<script
    src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular-route.js"></script>
 <script src='test.js' type="text/javascript"></script>
</head>

<body >
    <div ng-view></div>
    <script src='test.js' type="text/javascript"></script>
</body>
</html>

Your new route configuration

您的新路线配置

applog.config([ '$routeProvider', '$locationProvider',
    function($routeProvider, $locationProvider) {
        $routeProvider.when('/home', {
            templateUrl : 'home.html',
            controller : 'HomeController'
        })
        $routeProvider.when('/', {
            templateUrl : 'auth.html',
            controller : 'LoginController'
        }).otherwise({
            redirectTo : 'index.html'
        });
        //$locationProvider.html5Mode(true); //Remove the '#' from URL.
    }
]);

and put your login code inside the auth.html file

并将您的登录代码放在auth.html文件中

<div id='content' ng-controller='LoginController'>
        <div class="container">
            <form class="form-signin" role="form" ng-submit="login()">
                <h3 class="form-signin-heading">Login Form</h3>
                <span><b>Username :</b>&nbsp; <input type="username"
                    class="form-control" ng-model="user.name" required> </span> </br>
                </br> <span><b>Password :</b>&nbsp;&nbsp; <input type="password"
                    class="form-control" ng-model="user.password" required> </span> 
                <br><br>                
                <button class="btn btn-lg btn-primary btn-block" type="submit">
                    <b>Sign in</b>
                </button>
            </form>
        </div>
        <!-- /container -->
    </div>

Have in mind that if you place the test.js script in index it will be in the whole app.

请记住,如果将test.js脚本放在索引中,它将在整个应用程序中。

Hope it helps

希望能帮助到你

#2


5  

Index.html

<html ng-app='myApp'>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"/>
<script
    src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>
<script
    src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular-route.js"></script>
</head>

<body >
    <div ng-view></div>
    <script src='test.js' type="text/javascript"></script>
</body>
</html>

test.js

var applog = angular.module('myApp',['ngRoute']);

applog.config([ '$routeProvider', '$locationProvider',
    function($routeProvider, $locationProvider) {
        $routeProvider.when('/home', {
            templateUrl : 'home.html',
            controller : 'HomeController'
        })
        $routeProvider.when('/', {
            templateUrl : 'login.html',
            controller : 'LoginController'
        }).otherwise({
            redirectTo : 'index.html'
        });
        //$locationProvider.html5Mode(true); //Remove the '#' from URL.
    }
]);

applog.controller("LoginController", function($scope, $location) {
    $scope.login = function() {
        var username = $scope.user.name;
        var password = $scope.user.password;
        if (username == "admin" && password == "admin") {
            $location.path("/home" );
        } else {
            alert('invalid username and password');
        }
    };
});

applog.controller("HomeController", function($scope, $location) {

});

login.html

<div id='content' ng-controller='LoginController'>
    <div class="container">
        <form class="form-signin" role="form" ng-submit="login()">
            <h3 class="form-signin-heading">Login Form</h3>
            <span><b>Username :</b>&nbsp; <input type="username"
                class="form-control" ng-model="user.name" required> </span> </br>
            </br> <span><b>Password :</b>&nbsp;&nbsp; <input type="password"
                class="form-control" ng-model="user.password" required> </span>
            <br><br>
            <button class="btn btn-lg btn-primary btn-block" type="submit">
                <b>Sign in</b>
            </button>
        </form>
    </div>
    <!-- /container -->
</div>

#1


5  

As @dfsq said you need an ng-view for placing there your new view. Of course if you add your ng-view in the index you will see the content of the index and of home. The solution here is creating two partial views, one for the log in authentication and the other for home. The index file should contain only the ng-view and those elements you want to keep constant in your whole app (menu, footer...)

正如@dfsq所说,你需要一个ng-view来放置你的新视图。当然,如果您在索引中添加ng-view,您将看到索引和home的内容。这里的解决方案是创建两个部分视图,一个用于登录身份验证,另一个用于主页。索引文件应仅包含ng-view以及要在整个应用程序中保持不变的元素(菜单,页脚...)

Here is the code for what I'm saying: index.html

这是我所说的代码:index.html

<html ng-app='myApp'>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"/>
<script
    src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>
<script
    src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular-route.js"></script>
 <script src='test.js' type="text/javascript"></script>
</head>

<body >
    <div ng-view></div>
    <script src='test.js' type="text/javascript"></script>
</body>
</html>

Your new route configuration

您的新路线配置

applog.config([ '$routeProvider', '$locationProvider',
    function($routeProvider, $locationProvider) {
        $routeProvider.when('/home', {
            templateUrl : 'home.html',
            controller : 'HomeController'
        })
        $routeProvider.when('/', {
            templateUrl : 'auth.html',
            controller : 'LoginController'
        }).otherwise({
            redirectTo : 'index.html'
        });
        //$locationProvider.html5Mode(true); //Remove the '#' from URL.
    }
]);

and put your login code inside the auth.html file

并将您的登录代码放在auth.html文件中

<div id='content' ng-controller='LoginController'>
        <div class="container">
            <form class="form-signin" role="form" ng-submit="login()">
                <h3 class="form-signin-heading">Login Form</h3>
                <span><b>Username :</b>&nbsp; <input type="username"
                    class="form-control" ng-model="user.name" required> </span> </br>
                </br> <span><b>Password :</b>&nbsp;&nbsp; <input type="password"
                    class="form-control" ng-model="user.password" required> </span> 
                <br><br>                
                <button class="btn btn-lg btn-primary btn-block" type="submit">
                    <b>Sign in</b>
                </button>
            </form>
        </div>
        <!-- /container -->
    </div>

Have in mind that if you place the test.js script in index it will be in the whole app.

请记住,如果将test.js脚本放在索引中,它将在整个应用程序中。

Hope it helps

希望能帮助到你

#2


5  

Index.html

<html ng-app='myApp'>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"/>
<script
    src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>
<script
    src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular-route.js"></script>
</head>

<body >
    <div ng-view></div>
    <script src='test.js' type="text/javascript"></script>
</body>
</html>

test.js

var applog = angular.module('myApp',['ngRoute']);

applog.config([ '$routeProvider', '$locationProvider',
    function($routeProvider, $locationProvider) {
        $routeProvider.when('/home', {
            templateUrl : 'home.html',
            controller : 'HomeController'
        })
        $routeProvider.when('/', {
            templateUrl : 'login.html',
            controller : 'LoginController'
        }).otherwise({
            redirectTo : 'index.html'
        });
        //$locationProvider.html5Mode(true); //Remove the '#' from URL.
    }
]);

applog.controller("LoginController", function($scope, $location) {
    $scope.login = function() {
        var username = $scope.user.name;
        var password = $scope.user.password;
        if (username == "admin" && password == "admin") {
            $location.path("/home" );
        } else {
            alert('invalid username and password');
        }
    };
});

applog.controller("HomeController", function($scope, $location) {

});

login.html

<div id='content' ng-controller='LoginController'>
    <div class="container">
        <form class="form-signin" role="form" ng-submit="login()">
            <h3 class="form-signin-heading">Login Form</h3>
            <span><b>Username :</b>&nbsp; <input type="username"
                class="form-control" ng-model="user.name" required> </span> </br>
            </br> <span><b>Password :</b>&nbsp;&nbsp; <input type="password"
                class="form-control" ng-model="user.password" required> </span>
            <br><br>
            <button class="btn btn-lg btn-primary btn-block" type="submit">
                <b>Sign in</b>
            </button>
        </form>
    </div>
    <!-- /container -->
</div>