javascript - AngularJS 中如何从另一个服务调用一个服务?

标签 javascript angularjs

我正在学习 Angular,我将 LocalStorage 服务定义为:

.service('LocalStorage', function() {
    var self = this;

    this.localStorageAvailable = function() {
        return typeof (Storage) !== "undefined";
    };

    this.set = function(key, value) {
        if (!self.localStorageAvailable()) return;
        localStorage.setItem(key, JSON.stringify(value));
    };

    this.get = function(key, defaultValue) {
        if (!self.localStorageAvailable()) return defaultValue;
        return JSON.parse(localStorage.getItem(key));
    };
})

然后我想创建另一个使用我刚刚定义的 LocalStorage 服务的服务,但它给出了:

error: $LocalStorage is udefined.

.service('FilterSvc', ['LocalStorage', function ($LocalStorage) {
    var self = this;
    var key = "Settings";
    this.LoadSettings = function (universeCode) {
        if ($LocalStorage.get(key, "") === "") {
            ////load setting, otherwise reuse
            var result = "aaa";
            this.set(key, result);
        }
    };
}]);

有人可以帮忙吗?

最佳答案

您应该从变量 $LocalStorage 中删除 $

关于javascript - AngularJS 中如何从另一个服务调用一个服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31814915/

相关文章:

javascript - AngularJS ng-repeat 和 $index 提供解析错误

javascript - 将 ng-message 与 ng-required 一起使用

javascript - 我怎样才能获得有关文档的最重要信息以及我在 MarkLogic/Query Console 中搜索的单词周围的一些行?

javascript - 不使用 Jquery 检查所有单选按钮

Joomla 2.5 中的 Javascript 移动重定向

javascript - 未捕获类型错误 : Cannot read property of undefined

javascript - 当输入值更改时,将多个输入字符串推送到范围数组 (AngularJS)

jquery - 使用没有 boostrap 的 angularjs 作为 "front-end ui framework"

javascript - 将UTC时间戳从数据库转换为登录日期时间的用户

Javascript/JSON : get name of specified object as string