localStorageService.set返回“未定义isUndefined”

时间:2015-01-20 10:48:23

标签: javascript angularjs

在我的AngularJs应用程序中,我尝试使用localStorage服务,我已经参考了" angular-local-storage.js"并将服务注入模块

var app = angular.module('SampleApp', ['ngRoute', 'ngSanitize', 'toaster', 'LocalStorageModule']);

当我试图在我的控制器中使用服务时抛出错误。

(function () {

var app = angular.module('SampleApp');

app.controller('loginController',  ['$scope', 'notificationFactory', '$location', 'HTMLEditorService', '$rootScope', '$q', 'localStorageService',
function ($scope, notificationFactory, $location, HTMLEditorService, $rootScope, $q, localStorageService) {

    var _authentication = {
        isAuth: false,
        userName: ""
    };

    $scope.bind = function (data) {
        if (data.status) {
            if (data.Metadata.ErrorMessage == null || data.Metadata.ErrorMessage.trim() == '') {

                if (data.Metadata.AuthToken == null || data.Metadata.AuthToken.trim() == '') {
                    notificationFactory.error("User authentication failed. Please try again.");
                }
                else {
                    var deferred = $q.defer();
                    localStorageService.set('authorizationData', {  userName: data.Data.UserName });

                    _authentication.isAuth = true;
                    _authentication.userName = data.Data.userName;

                    deferred.resolve(response);

                    notificationFactory.success('User logged-in successfully!');
                    $rootScope.isLoggedIn = true;
                    $location.url('/LandingPage');
                }
            }
            else {
                notificationFactory.error(data.Metadata.ErrorMessage);
            }
        } else {
            notificationFactory.error('Server Error. Please try again.');
        }
    }

    $scope.userLogin = function () {
        try {

            if (loginValidation(this.userName, this.password)) {

                var request = new Object();
                request.Metadata = new Object();
                request.Data = new Object();

                request.Metadata.SecurityGroupId = 1;

                request.Data.UserName = this.userName;
                request.Data.Password = this.password;

                HTMLEditorService.userLogin(request)
                    .then($scope.bind);
            }
            else {
                notificationFactory.error('Invalid credentials');
            }
        } catch (e) {
            notificationFactory.error('Error! Please try again.');
            console.log(e);
        }
    }


    $scope.cancelLogin = function () {
        try {
            this.userName = '';
            this.password = '';

            $location.url("/Login");
        } catch (e) {
            notificationFactory.error('Error! Please try again.');
        }
    }
    $rootScope.isLoggedIn = false;
}]);

}());

错误抛出:

ReferenceError:未定义isUndefined

任何建议都非常受欢迎。

提前致谢

2 个答案:

答案 0 :(得分:9)

请参阅github问题here

简而言之,您可能会在其主页上引用该版本,而该主页并未包含该行

  

var isDefined = angular.isDefined

解决方案是使用github中的版本,或者最好使用bower

进行安装
  

bower install angular-local-storage --save

答案 1 :(得分:0)

我遇到了同样的问题。 我通过添加前缀“angular”来解决它。到angular-local-storage.js中的isObject,isUndefined,isArray和isNumber:

angular.isObject, angular.isUndefined ...