如何在angularjs

时间:2017-01-20 11:25:23

标签: javascript php angularjs cookies

我在使用cookies方面遇到了麻烦。当我在我的项目中使用ngCookies时,我的div(警报)出现了问题。 enter image description here

这是我的app.js

angular.module('postLogin', [ngCookies])
.controller('PostController', ['$scope', '$http', function($scope, $http) {        
        this.postForm = function() {
            var encodedString = 'username=' +
                encodeURIComponent(this.inputData.username) +
                '&password=' +
                encodeURIComponent(this.inputData.password);

            $http({
                method: 'POST',
                url: 'userauth.php',
                data: encodedString,
                headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            })

            .success(function(data) {
                    //console.log(data);
                    if ( data.trim() === 'correct') {
                        window.location.href = 'success.html';
                    } else {
                        $scope.errorMsg = "Username and password do not match.";
                    }
            })            
        }
}]);

这是我的index.html

    <!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="utf-8"/>
      <meta name="robots" content="noindex"/>
      <title>angulrjs login page</title>
      <meta name="viewport" content="width=device-width, initial-scale=1"/>
      <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"/>
      <link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css" rel="stylesheet" id="main-css"/>
      <link href="css/style.css" rel="stylesheet" id="main-css"/>
      <script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
      <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>  
      <script src="angular.min.js"></script>
   </head>
   <body ng-app="postLogin" ng-controller="PostController as postCtrl">
      <div class="container">
         <div id="loginbox" class="mainbox col-md-6 col-md-offset-3 col-sm-6 col-sm-offset-3">
            <div class="row">
               <img src="images/logo.jpg" class="imglogo"/>
            </div>
            <div class="panel panel-default" >
               <div class="panel-heading">
                  <div class="panel-title text-center">Login using username & password</div>
               </div>
               <div class="panel-body" >
                  <form name="login" ng-submit="postCtrl.postForm()" class="form-horizontal" method="POST">
                     <div class="input-group">
                        <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                        <input type="text" id="inputUsername" class="form-control" required autofocus ng-model="postCtrl.inputData.username"/>
                     </div>
                     <div class="input-group">
                        <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
                        <input type="password" id="inputPassword" class="form-control" required ng-model="postCtrl.inputData.password"/>
                     </div>
                     <div class="alert alert-danger" ng-show="errorMsg">
                        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">
                        ×</button>
                        <span class="glyphicon glyphicon-hand-right"></span>&nbsp;&nbsp;{{errorMsg}}
                     </div>
                     <div class="form-group">
                        <div class="col-sm-12 controls">
                           <button type="submit" class="btn btn-primary pull-right" ng-disabled="login.$invalid">
                           <i class="glyphicon glyphicon-log-in"></i> Log in</button>
                        </div>
                     </div>
                  </form>
               </div>
            </div>
         </div>
      </div>
      <script src="app.js"></script>
   </body>
</html>

当我没有使用ngCookies时

angular.module('postLogin', [])

它可以正常运行。请帮助我,我真的希望它将我的登录表单中的用户名扔到下一页。我不能在php中使用像会话这样的$ cookies。感谢。

1 个答案:

答案 0 :(得分:0)

在模块设置器(angular.module ...)中,当您注入依赖模块时,它们必须在引号中。在您的示例中,ngCookies周围没有引号。此外,您的index.html似乎没有引用angular-cookies.js文件,该文件不是基本angular.js文件的一部分。

检查开发工具是否存在特定错误(F12)。