简体   繁体   English

Angular提供程序未加载

[英]Angular provider not loading

i'm having some issues loading my providers. 我在加载我的提供程序时遇到了一些问题。

setup is as follows: 设置如下:

  • ng-view with main.html ng-view与main.html
  • main.html has controller MainCtrl main.html具有控制器MainCtrl
  • I have several directives which need access to the same data, so i'm using a provider to get it. 我有几个指令需要访问相同的数据,所以我正在使用提供程序来获取它。
  • I have one provider called ItemStorage that is a generic provider used to write, read, etc. anything based on parameters 我有一个称为ItemStorage的提供程序,该提供程序是用于根据参数写入,读取等任何内容的通用提供程序。
  • I have another provider that references ItemStorage that offers methods for getting specific things from ItemStorage called Data 我还有一个引用ItemStorage的提供程序,该提供程序提供了从ItemStorage中获取特定内容的方法,称为Data

When i reference Data in my controller (which references ItemStorage) I get a: "Error: [$injector:unpr] Unknown provider: ItemStorage" 当我在控制器(引用ItemStorage)中引用数据时,我得到一个错误:[错误:[$ injector:unpr]未知提供程序:ItemStorage”

but when I remove Data's reference to ItemStorage the application executes perfectly (minus some undefined errors caused by pointing to an empty object) 但是,当我删除Data对ItemStorage的引用时,应用程序会完美执行(减去一些由于指向空对象而导致的未定义错误)

According to JSHint I have no syntax errors. 根据JSHint,我没有语法错误。 Below is my providers file any help would be appreciated! 以下是我的提供商文件,将不胜感激!

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);
};
})

this is just a limited subset of the code. 这只是代码的有限子集。

While writing this up I have done some reading.. it is not possible to update a provider during run-time? 在撰写本文时,我已经进行了一些阅读..在运行时无法更新提供程序吗? can I not call a method to save something to local storage? 我不能调用将某些内容保存到本地存储的方法吗?

ultimately I need something that can hold data for the current user that is accessible and can be updated by any element to update every other element.. is this possible? 最终,我需要一些可以保存当前用户数据的东西,该数据可以访问并且可以由任何元素更新以更新所有其他元素。

When comparing to the documentation it would appear that you have not named your function and this could be the issue. 与文档进行比较时,您似乎没有命名函数,这可能是问题所在。 So try something like: 因此,尝试类似:

angular.module('myApp').provider('itemStorage', function itemStorageProvider(){

this.$get = function(){ this。$ get = function(){

};

}) })

You probably also need to add this to you config, along the lines of 您可能还需要按照以下步骤将其添加到配置中

myApp.config(["itemStorageProvider", function(itemStorageProvider) {

}]); }]);

Taken loosely from Provider docs from angular website 从Angular网站的Provider文档中轻松获取

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM