2015-10-05 38 views
2

我得到的,这个错误角注射来自不同模块

错误:[NG:AREQ] http://errors.angularjs.org/1.4.7/ng/areq?p0=fn&p1=not%20a%20function%2C%20got%20undefined

,当我尝试以下方法

HTML

<div ng-app="angularmodule"> 
    <div ng-controller="mycontroller"> 
     <div class="mydirective" </div> 
    </div> 

指令

angular 
    .module('ui.module', []) 
    .directive('mydirective', function() { 

     //some directive stuff in here, restricted as ‘C’ 

      } 
     }; 
    }); 

angular 
    .module('dataLayerModule', []) 
    .factory('datalayer', [$http], 

     function (http) { 

     //some method in here that uses http request 
      return factory; 
     }); 

控制器

angular 
    .module(‘angularmodule’, [‘ui.module', 'dataLayerModule']) 
    .controller('mycontroller', ['$scope', 'datalayer', function ($scope, datalayer) { 

     //this is dependent on two modules being injected i.e. ‘ui.module','dataLayerModule' 
    }]); 

如果我从刚剥离出工厂( 'dataLayerModule')模块注射它上面的作品如

angular 
     .module('angularmodule’, ['ui.module']) 
     .controller('mycontroller', ['$scope', function ($scope) { 


     }]); 

问题是我的“dataLayerModule '注入,而'数据层'工厂没有被注入,我想。 上面的角度JavaScript代码是在不同的文件,但正确加载在HTML中。

我试图做的是注入出厂到控制器,工厂将负责通过AJAX获取JSON数据

之所以有这些不同的模块是,“UI模块”来自我无法控制的第三方。

如果任何人都可以创建一个plunker,与此注射仅仅运行/加载正确的,我会很感激,这是推动我疯了

+0

的问题可能是你 'dataLayerModule' – Nicolas2bert

+0

内即使我从dataLayer剥离它的代码仍然错误,$ http以及 – Rory

+0

您使用的引号,看起来不同,请检查它 – Nicolas2bert

回答

1

在你factory声明,你说:

.factory('datalayer', [$http], function (http) {}) 

但我认为这是你想要什么:

.factory('datalayer', ['$http', function (http) {}]) 
1

你有你如何注入$http依赖到语法错误的最有可能破坏工厂代码的工厂。您目前没有在注入依赖项的数组中包含工厂方法。在$http周围也没有引号,它没有正确注入工厂(当它作为参数传递给函数时,没有$符号)。它应该是:

angular 
.module('dataLayerModule', []) 
.factory('datalayer', ['$http', function($http) { 

    //some method in here that uses http request 
    return factory; 
}]); 

替代语法

利用上述的语法(包括依赖关系,并在阵列内的功能),它可以是很难发现这些类型的错误。另一种更明确的语法是将函数名称传递给工厂方法,并进一步声明该函数。

angular 
    .module('dataLayerModule', []) 
    .factory('datalayer', dataLayer); 

dataLayer.$inject = ['$http']; 

function dataLayer($http) { 

    //some method in here that uses http request 
    return factory; 
}