gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 09:21:53 25 4
gpt4 key购买 nike

我正在学习 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com