$ injector:modulerr:AngularJS应用程序中的身份验证 [英] $injector:modulerr : authentication in AngularJS applications

查看:83
本文介绍了$ injector:modulerr:AngularJS应用程序中的身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难传递此错误:


未捕获错误:[$ injector:modulerr]

当我尝试使用AngularJS制作身份验证应用程序。我把代码放在 plunker 中。

when I am trying to make an authentication app with AngularJS. I put the code in plunker.

以下是我的代码:

1- app.js

1- app.js

"use strict";
(function() {

var app = angular.module('loginApp', ['AuthService', 'Session']);
app.constant('appSettings', {
    title:'Authentication Application',
    version:'1.0'
});
app.constant('AUTH_EVENTS', {
      loginSuccess: 'auth-login-success',
      loginFailed: 'auth-login-failed',
      logoutSuccess: 'auth-logout-success',
      sessionTimeout: 'auth-session-timeout',
      notAuthenticated: 'auth-not-authenticated',
      notAuthorized: 'auth-not-authorized'
});
app.constant('USER_ROLES', {
      all: '*',
      admin: 'admin',
      editor: 'editor',
      guest: 'guest'
});
app.controller('footerController', function($scope, appSettings){
    $scope.appSettings = appSettings;
});

}());

2 - applicationcontroller.js

2 - applicationcontroller.js

"use strict";
(function() {

var applicationcontroller = function ($scope, USER_ROLES, AuthService) {
      $scope.currentUser = null;
      $scope.userRoles = USER_ROLES;
      $scope.isAuthorized = AuthService.isAuthorized;

      $scope.setCurrentUser = function (user) {
        $scope.currentUser = user;
      };
};

applicationcontroller.$inject = ['$scope', 'USER_ROLES', 'AuthService'];

angular.module('loginApp')
  .controller('applicationcontroller', applicationcontroller);

}());

3- logincontroller.js

3- logincontroller.js

"use strict";
(function() {

var logincontroller = function ($scope, $rootScope, AUTH_EVENTS, AuthService) {
      $scope.credentials = {
        username: '',
        password: ''
      };
      $scope.login = function(credentials){
        AuthService.login(credentials).then(function(user){
            $rootScope.$broadcast(AUTH_EVENTS.loginSuccess);
            $scope.setCurrentUser(user);
        }, function(error){
            $rootScope.$broadcast(AUTH_EVENTS.loginFailed);
        });
      };
};

logincontroller.$inject = ['$scope', '$rootScope', 'AUTH_EVENTS', 'AuthService'];

angular.module('loginApp')
  .controller('logincontroller', logincontroller);

}());

4- authservice.js

4- authservice.js

"use strict";
(function(){
   var AuthService = function($http, Session){
    var authService = {};

    authService.login = function (credentials) {
      return $http
        .post('/login', credentials)
        .then(function (res) {
          Session.create(res.data.id, res.data.user.id,
                         res.data.user.role);
          return res.data.user;
        });
    };

    authService.isAuthenticated = function () {
      return !!Session.userId;
    };

    authService.isAuthorized = function (authorizedRoles) {
      if (!angular.isArray(authorizedRoles)) {
        authorizedRoles = [authorizedRoles];
      }
      return (authService.isAuthenticated() &&
        authorizedRoles.indexOf(Session.userRole) !== -1);
    };

    return authService;
};
AuthService.$inject = ['$http', 'Session'];
angular.module('loginApp').factory('AuthService', AuthService);
}());

5- session.js

5- session.js

"use strict";
(function(){
var Session = function(){
        this.create = function (sessionId, userId, userRole) {
        this.id = sessionId;
        this.userId = userId;
        this.userRole = userRole;
      };
      this.destroy = function () {
        this.id = null;
        this.userId = null;
        this.userRole = null;
      };
};
angular.module('loginApp').service('Session', Session);
}());

6- index.html

6- index.html

<!DOCTYPE html>
<html data-ng-app="loginApp" lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Authentication</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
<link href="css/main.css" rel="stylesheet">
</head>
<body ng-controller="applicationcontroller">
<div class="container">
    <div class="jumbotron">
    <form name="loginForm" ng-controller="logincontroller" ng-submit="login(credentials)" novalidate>
      <div class="form-group">
            <label for="username">Username:</label>
            <input type="text" id="username" ng-model="credentials.username" class="form-control">
     </div>
     <div class="form-group">      
      <label for="password">Password:</label>
      <input type="password" id="password" ng-model="credentials.password" class="form-control">
    </div>
       <button type="submit" class="btn btn-default">Submit</button>

    </form>
    <hr class="color-violet">
    <footer class="text-center" data-ng-controller="footerController"> MADE WITH <i class="fa fa-heart color-violet"></i> BY <span class="color-violet">ALAN SABERI</span>. FIND THIS ON <a href="https://github.com/alireza-saberi/customer-application" target="_bank"><span class="color-violet">GITHUB</span></a><div>Version: {{appSettings.version}}</div></footer>
</div>
</div>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
     <!-- Include all compiled plugins (below), or include individual files as needed -->
     <script src="vendor/bootstrap.min.js"></script>
     <script src="vendor/angular.min.js"></script>
     <script arc="js/services/authservice.js"></script>
     <script arc="js/services/session.js"></script>
     <script src="js/app.js"></script>
     <script src="js/controllers/applicationcontroller.js"></script>       
     <script src="js/controllers/logincontroller.js"></script>
  </body>
</html>


推荐答案

首先,在引用脚本时,您会输入拼写错误文件

First of all you have a typo when referencing your script files

<script arc="js/services/authservice.js"></script>
<script arc="js/services/session.js"></script>

应该是

<script src="js/services/authservice.js"></script>
<script src="js/services/session.js"></script>

第二 - >您的AuthService和Session不是新模块,它们在同一个loginApp模块中注册根据您的代码。因此,您不要将它们注入loginApp模块。

Second -> Your AuthService and Session are not new modules, they are being registered in the same loginApp module based on your code. So you don't inject them into your loginApp module.

更改

var app = angular.module('loginApp', ['AuthService', 'Session']);

到此

var app = angular.module('loginApp', []);

第三 - >在加载app.js之前加载服务脚本文件,记住app.js是您首先定义用于分配服务的loginApp模块的位置,因此将脚本加载顺序更改为此

Third -> You are loading your service script files before loading your app.js, remember app.js is where you are first defining your loginApp module that you are using to assign your services, so change your script load order to be this

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
         <!-- Include all compiled plugins (below), or include individual files as needed -->
         <script src="bootstrap.min.js"></script>
         <script src="angular.min.js"></script>
         <script src="app.js"></script>
         <script src="session.js"></script>
         <script src="authservice.js"></script>             
         <script src="applicationcontroller.js"></script>       
         <script src="logincontroller.js"></script>

这是你的分叉和工作的plnkr: http://plnkr.co/edit/5BEIsVxwt8sKwr4HA8Eq?p=preview

Here's your plnkr that's forked and working : http://plnkr.co/edit/5BEIsVxwt8sKwr4HA8Eq?p=preview

这篇关于$ injector:modulerr:AngularJS应用程序中的身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆