在角度js中添加自定义服务时出错

时间:2016-05-31 05:45:46

标签: javascript angularjs angular-services

我是AngularJS的新手,我已经设置了角度js中的所有内容,但是当我要添加新的身份验证服务时,我会收到这样的错误:

enter image description here

我在会话中创建了名为 sessionService.js 的服务并获取了用户值 我创建了名为 authService.js 的服务来检查用户身份验证并获取用户名等等

这是 sessionService.js

'usre strict';

app.factory('sessionService', ['$http', function($http){
    return {
        set : function(key,value){
            return sessionStorage.setItem(key,value);
        },
        get : function(key){
            return sessionStorage.getItem(key);
        },
        destroy : function(key){
            return sessionStorage.removeItem(key);
        }
    }
}])

这是 authService.js

'use strict';

app.factory('authService', ['$http','sessionService', function($http,sessionService){
      var loggedIn;
    var isAdmin;
    var username;
    var id;

    var checkLogin = function() {
        // Create a custom deferred
        var defer = $q.defer();

        if(loggedIn != undefined) {
            // Resolve your deferred with the value of logged in
            defer.resolve(loggedIn);
        } else {
            setUserData().then(function (data) {
               loggedIn = data.logged;
               if(loggedIn) {
                  isAdmin = data.is_admin;
                  username = data.username;
                  id = data.id;
                  defer.resolve(loggedIn);
               } else {
                  defer.reject();
               }
            }, function () {
               defer.reject();
            });
        }

        return defer.promise;
    }

    var returnUsername = function() {
        // Create a custom deferred
        var defer = $q.defer();

        if(username != undefined) {
            // Resolve your deferred with the value of logged in
            defer.resolve(username);
        } else {
            setUserData().then(function (data) {
               loggedIn = data.logged;
               if(loggedIn) {
                  username = data.username;
                  defer.resolve(username);
               } else {
                  defer.reject();
               }
            }, function () {
               defer.reject();
            });
        }

        return defer.promise;
    }

    var returnId = function() {
        // Create a custom deferred
        var defer = $q.defer();

        if(id != undefined) {
            // Resolve your deferred with the value of logged in
            defer.resolve(id);
        } else {
            setUserData().then(function (data) {
               loggedIn = data.logged;
               if(loggedIn) {
                  id = data.id;
                  defer.resolve(id);
               } else {
                  defer.reject();
               }
            }, function () {
               defer.reject();
            });
        }

        return defer.promise;
    }

    var setUserData = function(callback) {
        return $http.get('/api/checkLogin');
    }

    return {
        loggedIn: function() { return checkLogin(); },
        username: function() { return returnUsername(); },
        id: function() { return returnId(); },
    }
}])

我在index.html中也添加了这两项服务

  <script src="js/app.js"></script>
  <script src="js/config.js"></script>
  <script src="js/config.lazyload.js"></script>
  <script src="js/config.router.js"></script>
  <script src="js/main.js"></script>
  <script src="js/services/ui-load.js"></script>
  <script src="js/services/sessionService.js"></script>
  <script src="js/services/authService.js"></script>
  <script src="js/filters/fromNow.js"></script>
  <script src="js/directives/setnganimate.js"></script>
  <script src="js/directives/ui-butterbar.js"></script>
  <script src="js/directives/ui-focus.js"></script>
  <script src="js/directives/ui-fullscreen.js"></script>
  <script src="js/directives/ui-jq.js"></script>
  <script src="js/directives/ui-module.js"></script>
  <script src="js/directives/ui-nav.js"></script>
  <script src="js/directives/ui-scroll.js"></script>
  <script src="js/directives/ui-shift.js"></script>
  <script src="js/directives/ui-toggleclass.js"></script>
  <script src="js/controllers/bootstrap.js"></script>

app.js

'use strict';


angular.module('app', [
    'ngAnimate',
    'ngAria',
    'ngCookies',
    'ngMessages',
    'ngResource',
    'ngSanitize',
    'ngTouch',
    'ngStorage',
    'ui.router',
    'ui.bootstrap',
    'ui.utils',
    'ui.load',
    'sessionService',
    'authService',
    'ui.jq',
    'oc.lazyLoad',
    'pascalprecht.translate',
]);

我需要帮助才能解决这个问题 我正在使用Angular Bootstrap Admin面板。
任何帮助将不胜感激。

谢谢

3 个答案:

答案 0 :(得分:0)

错误在于您将sessionServiceauthService视为一个模块,并将其作为依赖项加载到app中。它们是服务而不是模块。即使是错误也是如此。服务可以注入其他服务,但不能作为模块依赖项加载。

更改app.js中的代码以删除服务,如下所示:

'use strict';


var app = angular.module('app', [
  'ngAnimate',
  'ngAria',
  'ngCookies',
  'ngMessages',
  'ngResource',
  'ngSanitize',
  'ngTouch',
  'ngStorage',
  'ui.router',
  'ui.bootstrap',
  'ui.utils',
  'ui.load',
  'ui.jq',
  'oc.lazyLoad',
  'pascalprecht.translate',
]);

答案 1 :(得分:0)

将app.js脚本包含在所有其他脚本之后。

答案 2 :(得分:0)

您的应用是否已全局实例化以用于其他js文件。我们说

App.js

var app = angular.module('app',[dependencies]);
$(function(){
//Other code
}());

在其他js文件中使用实例化的应用程序。