angularjs $ localStorage未定义,但已定义

时间:2015-02-18 02:15:43

标签: javascript angularjs angular-local-storage

我正在使用angularjs的$localStorage函数,但目前它不希望因某些原因而被包含在内。我知道$localStorage可以工作,因为我在同一个webapp中的其他控制器中使用它。我也知道这不是拼写错误,因为我反复检查过,我也知道$localStorage变量curAllianceCol已定义,因为我在后面的控制器中使用了这个完全相同的变量。 / p>

任何帮助都表示赞赏,如果(由于某种原因,我不知道)你不能在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.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控制$注射器< @ 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   更新视图@ 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 /角-UI-路由器/释放/角-UI-router.js:3801:11   。$ RootScopeProvider / $这gethttp://192.168.250.111:8080 / bower_components /角度/ 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 /<。于是/< @ http://192.168.250.111:8080/bower_components/angular/angular.js:11768:26   。$ RootScopeProvider / $这gethttp://192.168.250.111:8080 / bower_components /角度/ angular.js:12811:16   。$ RootScopeProvider / $这gethttp://192.168.250.111:8080 / bower_components /角度/ angular.js:12623:15   。$ RootScopeProvider / $这gethttp://192.168.250.111:8080 / bower_components /角度/ angular.js:12915:13   做@ 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.js:8603:1   “

1 个答案:

答案 0 :(得分:1)

我不确定您的代码是否正确但是,您是否尝试删除$interval或添加相应的依赖项,因为这是传递到您的指令的第二个参数,这将使$localStorage(您的第3个)参数)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..)
}]);