我在加载我的提供程序时遇到一些问题。
设置如下:
- 带有 main.html 的 ng-view
- main.html 有 Controller MainCtrl
- 我有几个指令需要访问相同的数据,因此我使用提供程序来获取它。
- 我有一个名为 ItemStorage 的提供程序,它是一个通用提供程序,用于根据参数写入、读取等任何内容
- 我有另一个引用 ItemStorage 的提供程序,它提供了从 ItemStorage 获取特定内容(称为 Data)的方法
当我在 Controller 中引用数据(引用 ItemStorage)时,我得到: “错误:[$injector:unpr] 未知提供者:ItemStorage”
但是当我删除 Data 对 ItemStorage 的引用时,应用程序可以完美执行(减去由于指向空对象而导致的一些未定义错误)
根据 JSHint,我没有语法错误。以下是我的提供商文件,如有任何帮助,我们将不胜感激!
angular.module('myApp')
.provider('itemStorage', function(){
this.$get = function(){
};
//some other methods
})
.provider('Data',function(itemStorage){
var ItemStorage = {};
this.arrays = ItemStorage.arrays;
this.$get = function(){
return {
foo: function(){ return this.getItemFromStorage(
};
};
this.getItemFromStorage = function(item){
return Storage.getItem(item);
};
})
这只是代码的有限子集。
在撰写本文时,我已经阅读了一些内容.. 无法在运行时更新提供程序?我可以不调用方法将某些内容保存到本地存储吗?
最终我需要一些可以保存当前用户数据的东西,这些数据可以访问并且可以由任何元素更新以更新每个其他元素。这可能吗?
最佳答案
与文档进行比较时,您似乎没有命名您的函数,这可能是问题所在。所以尝试这样的事情:
angular.module('myApp').provider('itemStorage', function itemStorageProvider(){
this.$get = 函数(){
};
})
您可能还需要将其添加到您的配置中,类似于
myApp.config(["itemStorageProvider", function(itemStorageProvider) {
}]);
关于javascript - Angular 提供程序未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23202426/