简体   繁体   English

未知提供程序:尝试使用Angular ngStorage时ngStorageProvider <-ngStorage

[英]Unknown provider: ngStorageProvider <- ngStorage when trying to use Angular ngStorage

I have this code below that uses ngStorage. 我下面有使用ngStorage的代码。 but when I try to run this is part of my angular app I get this error to the console: 但是当我尝试运行这是我的角度应用程序的一部分时,我将向控制台显示此错误:

Error: [$injector:unpr] Unknown provider: ngStorageProvider <- ngStorage <- signupController

Why? 为什么?

sign in controller.js 登录controller.js

var smallTalkzModel = angular.module('smallTalkzModel', ['ui.router', 'luegg.directives', 'ngCookies', 'ngStorage', 'angular-jwt']);

smallTalkzModel.controller('signupController', ['$scope', '$location', '$http', 'userDetails','ngStorage',
    function ($scope, $location, $http, userDetails,$localStorage) {

        $scope.register_user = function (info) {
            $http({
                url: '/register_user',
                method: 'POST',
                data: info
            }).then(function (response) {
                $localStorage.jwt = response.data.id_token;
                $location.path('main');
            }, function (error) {
                alert(error.data);
            });
        }
    }]);

update: 更新:

changed the code to include the $localStoragein the controller paramters. 更改了代码,以将$ localStorage包含在控制器参数中。 now the error mesge is gone, but the $localStorage is undefined after I make an assigmnet into it... 现在错误消息消失了,但是在我将一个assigmnet放入其中之后,$ localStorage是不确定的...

controller('signupController', ['$scope', '$location', '$http', 'userDetails', '$localStorage',
    function ($scope, $location, $http, userDetails, $localStorage) {

        $scope.login_info = "";
        $scope.userDetails = userDetails.isLogged;
        $scope.userLogin = false;
        $http.get('/online_users')
            .success(function (data) {
                $scope.usersNumber = data.length;
            })
            .error(function (data) {
                console.log('Error: ' + data);
            });

        $scope.register_user = function (info) {
            $http({
                url: '/register_user',
                method: 'POST',
                data: info
            }).then(function (response) {
                $localStorage.jwt = response.data.id_token;
                $location.path('main');
            }, function (error) {
                alert(error.data);
            });
        }

    }]);

this is where my code throw the error of undefined localstorage: 这是我的代码抛出未定义的本地存储错误的地方:

mainController.js mainController.js

smallTalkzModel.controller('mainController', ['$scope', 'sessionInfo', '$location', '$http', 'userDetails','$localStorage',
    function ($scope, sessionInfo, $location, $http, userDetails, jwtHelper,$localStorage) {

        $scope.login_info = "";
        $scope.userLogin = userDetails.isLogged;

        var jwt = $localStorage.jwt; // here $localStorage is undefined
..........

}

You need to install ngStorage, and add it to your project. 您需要安装ngStorage,并将其添加到您的项目中。 It is not part of angular core. 它不是角铁芯的一部分。

If you have it already installed, the error could be caused because you forgot to add the script in your project. 如果已经安装了该脚本,则可能是由于忘记了在项目中添加脚本而引起的错误。

Here the official documentation: https://github.com/gsklee/ngStorage 这里是官方文档: https : //github.com/gsklee/ngStorage

Here an example: How to use ngStorage in angularjs 这是一个示例: 如何在angularjs中使用ngStorage

UPDATE : 更新

I see than in the controller signupController you are injecting ngStorage (in the string part) when the injection should be $localStorage or $sessionStorage. 我看到在控制器signupController中,当注入应该是$ localStorage或$ sessionStorage时,您正在注入ngStorage(在字符串部分)。 This is causing the issue because ngStorage is a module, not a provider. 这是由于ngStorage是模块而不是提供程序导致的。

UPDATE 2 : 更新2

When you are declaring the mainController, the second parameter are the modules that your controller depends on, there, you forgot to add the string for jwtHelper, just before the String for $localStorage. 当声明mainController时,第二个参数是控制器依赖的模块,在那儿,您忘了在$ localStorage的字符串之前添加jwtHelper的字符串。 So there is one String less that variables you have in your function. 因此,您的函数中的变量比字符串少一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM