用户使用angular.js路由器和PHP登录后重定向到另一个页面

时间:2015-10-04 04:06:13

标签: php angularjs router

我需要一个帮助。我想在用户登录后使用angular.js重定向到另一个页面。我使用php作为后端,这里我想使用Angular router.I已经完成了一些编码,但它无法正常工作。我在下面解释我的代码。

  

的index.html:

<!DOCTYPE html>
<html lang="en" ng-app="Channabasavashwara">
<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>...:::WELCOME TO Channabasavashwara Institude of Technology:::...</title>

    <!-- PACE LOAD BAR PLUGIN - This creates the subtle load bar effect at the top of the page. -->
    <link href="css/pace.css" rel="stylesheet">
    <script src="js/pace.js"></script>
    <script src="js/angularjs.js" type="text/javascript"></script>
     <script src="controller/loginController.js" type="text/javascript"></script>
    <!-- GLOBAL STYLES - Include these on every page. -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href='http://fonts.googleapis.com/css?family=Ubuntu:300,400,500,700,300italic,400italic,500italic,700italic' rel="stylesheet" type="text/css">
    <link href='http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel="stylesheet" type="text/css">
    <link href="icons/font-awesome/css/font-awesome.min.css" rel="stylesheet">
    <link rel="shortcut icon" href="img/favicon.png">
    <!-- PAGE LEVEL PLUGIN STYLES -->
    <!-- THEME STYLES - Include these on every page. -->
    <link href="css/style.css" rel="stylesheet">
    <link href="css/plugins.css" rel="stylesheet">
    <link href="css/chosen.css" rel="stylesheet">

    <!-- THEME DEMO STYLES - Use these styles for reference if needed. Otherwise they can be deleted. -->
    <link href="css/demo.css" rel="stylesheet">

    <!--[if lt IE 9]>
      <script src="js/html5shiv.js"></script>
      <script src="js/respond.min.js"></script>
    <![endif]-->

<style type="text/css">
<!--
body{
    background-color:#FFF !important;
    }
.well {
    min-height: 20px;
    padding: 19px !important;
    margin-bottom: 20px;
    background-color: #f5f5f5;
    border: 1px solid #e3e3e3;
    border-radius: 4px;
    -webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,0.05);
    box-shadow: inset 0 1px 1px rgba(0,0,0,0.05);
    }
-->
</style>

</head>

<body>
    <!-- /#wrapper -->
    <div id="wrapper">
        <div class="container">
            <div class="row">
                <div class="col-md-12 text-center login-header" style="padding:40px 0px;">

                </div>
                <!--/span-->
            </div><!--/row-->
            <div class="col-md-6 marginautodiv floatnonediv well" ng-controller="loginController">
                <div class="totalaligndiv">
                <div class="alert alert-info text-center">

                    <span style="font-weight:bold; font-size:22px">Channabasavashwara Institude of Technology</span>
                    <BR>
                    Please login with your Username and Password.
                </div>
                 <form class="form-horizontal"  name="frmlogin" id="frmlogin" action="" autocomplete="off">
                    <fieldset>
                        <div class="input-group input-group-lg">
                            <span class="input-group-addon"><i class="fa fa-user"></i></span>
                            <input type="text" name="txtname" id="txtname" class="form-control" placeholder="Username" ng-model="user_name">
                        </div>
                        <div class="clearfix"></div><br>

                        <div class="input-group input-group-lg">
                            <span class="input-group-addon"><i class="fa fa-lock"></i></span>
                            <input type="password" name="pwd" id="pwd" class="form-control" placeholder="Password" ng-model="user_pass">
                        </div>
                        <div class="clearfix"></div>

                        <div class="input-prepend">
                        </div>
                        <div class="clearfix"></div>

                        <p class="text-center tbpaddingdiv2">
                        <button type="text" name="login" id="login" class="btn btn-primary"  style="width:270px;" ng-click="user_login();">Login</button>
                        </p>
                    </fieldset>
                </form>
                </div>
            </div>
        </div>
    </div>
    <!-- GLOBAL SCRIPTS -->
    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/jquery.slimscroll.min.js"></script>
    <script src="js/jquery.popupoverlay.js"></script>
    <script src="js/defaults.js"></script>
    <!-- Logout Notification Box -->

    <!-- /#logout -->
    <!-- Logout Notification jQuery -->
    <script src="js/logout.js"></script>
    <!-- HISRC Retina Images -->

    <!-- THEME SCRIPTS -->
    <script src="js/flex.js"></script>
    <script src="js/dashboard-demo.js"></script>
    <script src="js/chosen.jquery.js" type="text/javascript"></script>
    <script src="js/prism.js" type="text/javascript"></script>
    <script type="text/javascript">
    var config = {
      '.chosen-select'           : {},
      '.chosen-select-deselect'  : {allow_single_deselect:true},
      '.chosen-select-no-single' : {disable_search_threshold:10},
      '.chosen-select-no-results': {no_results_text:'Oops, nothing found!'},
      '.chosen-select-width'     : {width:"95%"}
    }
    for (var selector in config) {
      $(selector).chosen(config[selector]);
    }
    </script>

</body>
</html>

在此页面中,用户将登录,当点击登录按钮时,用户将重定向到dashboard页面。

  

loginController.js:

var loginAdmin=angular.module('Channabasavashwara',['ngRoute']);
loginAdmin.config(function($routeProvider){
    $routeProvider
    .when('/dashboard',{
        templateUrl: 'view/dashboard.html',
        controller: 'dashboardController'
    });
})
loginAdmin.controller('loginController',function($scope,$http){
    $scope.user_login=function(){
    if($scope.user_name==''){
        alert('user name filed should not keep blank');
    }else if($scope.user_pass==''){
        alert('password filed should not keep blank');
    }else{
        var userData={'user_name':$scope.user_name,'user_pass':$scope.user_pass};
        $http({
            method: 'POST',
            url: "php/Login/login.php",
            data: userdata,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).then(function successCallback(response){
            alert(response.data['msg']);
            location.href='#dashboard';
        },function errorCallback(response) {
            alert(response.data['msg']);
        });
    }
    }
});

我想使用路由器来路由页面。请帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

使用此

$location.path == '/dashboard';

而不是使用

location.href='#dashboard';

答案 1 :(得分:0)

您也可以使用路由

this.router.navigate(['/dashboard']);

在TS文件中。