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