AngularJS中$localStorage未定义,但已定义

3
我正在使用angularjs的$localStorage函数,但目前由于某些原因它无法被包含。我知道$localStorage是有效的,因为我已经在同一webapp的其他控制器中使用过它。我也知道这不是拼写错误,因为我多次检查过,我还知道$localStorage变量curAllianceCol被定义了,因为我在后面的控制器中使用了完全相同的变量。
任何帮助都将不胜感激,如果(出于某些我不知道的原因)您不能在angularjs指令中使用$localStorage,请告诉我,以便我可以想一个解决方法。
相关JS:
window.fires = angular.module('FIRES', ['ngResource', 'ui.bootstrap', 'ui.router', 'ngStorage']);
//(...Non-relevant code..)
window.fires.directive('fieldDirective', ['ScoutService', '$localStorage', function(ScoutService, $interval, $localStorage) {
    var $scope = this;
    $scope.sServ = ScoutService;
    console.log($localStorage.curAllianceCol); //This is line 177, just for reference between the error message and this file
    $scope.col = $localStorage.curAllianceCol;
    //(...Non-relevant code..)
}]);

我收到的错误信息:

“错误:$localStorage未定义 @http://192.168.250.111:8080/js/fires.js:177:2 调用@http://192.168.250.111:8080/bower_components/angular/angular.js:3965:14 registerDirective/http://192.168.250.111:8080/bower_components/angular/angular.js:5716:33 forEach@http://192.168.250.111:8080/bower_components/angular/angular.js:325:9 registerDirective/<@http://192.168.250.111:8080/bower_components/angular/angular.js:5714:13 调用@http://192.168.250.111:8080/bower_components/angular/angular.js:3965:14 createInjector/instanceCache.$injector<@http://192.168.250.111:8080/bower_components/angular/angular.js:3807:20 getService@http://192.168.250.111:8080/bower_components/angular/angular.js:3929:39 addDirective@http://192.168.250.111:8080/bower_components/angular/angular.js:6804:41 collectDirectives@http://192.168.250.111:8080/bower_components/angular/angular.js:6231:1 compileNodes@http://192.168.250.111:8080/bower_components/angular/angular.js:6080:22 compileNodes@http://192.168.250.111:8080/bower_components/angular/angular.js:6096:15 compileNodes@http://192.168.250.111:8080/bower_components/angular/angular.js:6096:15 compileNodes@http://192.168.250.111:8080/bower_components/angular/angular.js:6096:15 编译@http://192.168.250.111:8080/bower_components/angular/angular.js:6017:15 $ViewDirectiveFill/<.compile/<@http://192.168.250.111:8080/bower_components/angular-ui-router/release/angular-ui-router.js:3893:20 nodeLinkFn@http://192.168.250.111:8080/bower_components/angular/angular.js:6752:13 compositeLinkFn@http://192.168.250.111:8080/bower_components/angular/angular.js:6146:13 publicLinkFn@http://192.168.250.111:8080/bower_components/angular/angular.js:6042:30 updateView@http://192.168.250.111:8080/bower_components/angular-ui-router/release/angular-ui-router.js:3839:23 $ViewDirective/directive.compile/http://192.168.250.111:8080/bower_components/angular-ui-router/release/angular-ui-router.js:3801:11 $RootScopeProvider/this.$gethttp://192.168.250.111:8080/bower_components/angular/angular.js:13093:15 transitionTo/$state.transition<@http://192.168.250.111:8080/bower_components/angular-ui-router/release/angular-ui-router.js:3218:11 qFactory/defer/deferred.promise.then/wrappedCallback@http://192.168.250.111:8080/bower_components/angular/angular.js:11682:31 qFactory/ref/<.then/<@http://192.168.250.111:8080/bower_components/angular/angular.js:11768:26 $RootScopeProvider/this.$gethttp://192.168.250.111:8080/bower_components/angular/angular.js:12811:16 $RootScopeProvider/this.$gethttp://192.168.250.111:8080/bower_components/angular/angular.js:12623:15 $RootScopeProvider/this.$gethttp://192.168.250.111:8080/bower_components/angular/angular.js:12915:13 done@http://192.168.250.111:8080/bower_components/angular/angular.js:8450:34 completeRequest@http://192.168.250.111:8080/bower_components/angular/angular.js:8664:7 createHttpBackend/http://192.168.250.111:8080/bower_components/angular/angular.js:
1个回答

2
我不确定你的代码是否正确,但是你尝试过删除$interval或添加适当的依赖项吗?因为这是传递到指令中的第二个参数,这将使$localStorage(您的第三个参数)变为undefined没有$interval的示例:
window.fires.directive('fieldDirective', ['ScoutService', '$localStorage', function(ScoutService, $localStorage) {
    var $scope = this;
    $scope.sServ = ScoutService;
    console.log($localStorage.curAllianceCol); //This is line 177, just for reference between the error message and this file
    $scope.col = $localStorage.curAllianceCol;
    //(...Non-relevant code..)
}]);

谢谢您指出我犯的低级错误。我之前用过那个,但忘记从函数声明中删除它了。现在它已经完美地运行了。 - Burn_E99
@Burn_E99 没问题 :) - jungy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接