【问题标题】:angularjs $localStorage is undefined, but was definedangularjs $localStorage 未定义,但已定义
【发布时间】:2015-04-18 22:29:44
【问题描述】:

我正在使用 angularjs 的 $localStorage 函数,但目前由于某种原因它不想被包含在内。我知道$localStorage 有效,因为我在同一个 web 应用程序的其他控制器中使用过它。我也知道这不是拼写错误,因为我反复检查过,而且我也知道 $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.$injectorhttp://192.168.250.111:8080/bower_components/angular/angular.js:3807:20 获取服务@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 收集指令@http://192.168.250.111:8080/bower_components/angular/angular.js:6231:1 编译节点@http://192.168.250.111:8080/bower_components/angular/angular.js:6080:22 编译节点@http://192.168.250.111:8080/bower_components/angular/angular.js:6096:15 编译节点@http://192.168.250.111:8080/bower_components/angular/angular.js:6096:15 编译节点@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 href="http://192.168.250.111:8080/bower_components/angular-ui-router/release/angular-ui-router.js:3893:20" rel="nofollow" target="_blank">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 复合LinkFn@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/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.transitionhttp://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 href="http://192.168.250.111:8080/bower_components/angular/angular.js:11768:26" rel="nofollow" target="_blank">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 完成@http://192.168.250.111:8080/bower_components/angular/angular.js:8450:34 完成请求@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:8603:1 "

【问题讨论】:

    标签: javascript angularjs angular-local-storage


    【解决方案1】:

    我不确定您的代码是否正确,但是您是否尝试过删除 $interval 或添加适当的依赖项,因为这是传递给您的指令的第二个参数,这将使 $localStorage(您的第三个参数)@987654323 @。

    没有 $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 没问题 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-19
    • 2018-11-09
    • 2014-10-14
    • 2018-08-06
    • 2018-02-14
    • 2021-03-18
    • 1970-01-01
    相关资源
    最近更新 更多