我在下面有这个使用 ngStorage 的代码。但是当我尝试运行这是我的角度应用程序的一部分时,我在控制台收到了这个错误:
Error: [$injector:unpr] Unknown provider: ngStorageProvider <- ngStorage <- signupController
为什么?
登录控制器.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);
});
}
}]);
更新:
更改代码以在控制器参数中包含 $localStorage。现在错误消息消失了,但是在我对其进行分配后 $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);
});
}
}]);
这是我的代码抛出未定义本地存储错误的地方:
主控制器.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
..........
}