【问题标题】:How to use $ngCookies in angularjs如何在 angularjs 中使用 $ngCookies
【发布时间】:2017-01-20 21:37:58
【问题描述】:

我在使用 cookie 时遇到了麻烦。当我在我的项目中使用 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 之类的会话。谢谢。

【问题讨论】:

    标签: javascript php angularjs cookies


    【解决方案1】:

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

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

    【讨论】:

    • 抱歉,打错了。我的意思是当我在模块设置器 ['ngCookies'] 中使用时,无法正常运行。如果我使用 ['ngCookies'],当我签入控制台的 F12 部分(点 JS)时,[$injector:modulerr] 中会出现错误。否则使用 ['ngCookies'],该错误在控制台中消失(点 JS)。我想,我应该有 .js 文件,比如 ngCookies 的 angular-min.js。也许 ?我错了吗?
    • 谢谢你的帮助,我知道怎么解决了。我只需要 .js 的那些部分(angular-cookies.min.js 和 angular.min.js)。我的参考是tutorialsavvy.com/2014/11/angularjs-cookie-example.htmlstackoverflow.com/questions/10961963/…。这是第一次使用javascript,因为我是初学者。对不起我的愚蠢问题。 :(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-01
    • 2015-07-21
    • 1970-01-01
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多