我想将子模块注入主应用程序,但出现注入错误

(错误:[ng:areq] http://errors.angularjs.org/1.3.5/ng/areq?p0=SelectionCtrl&p1=not%20aNaNunction%2C%20got%20undefined

这是我的主要应用



这是我的子模块


我该如何解决?谢谢!

最佳答案

您搞砸了模块声明。您两次声明了angular.module('app.newProject')

首次创建时,您注册了SelectionCtrl。之后,您创建了另一个具有依赖关系的同名angular.module('app.newProject,[]')的模块并注册了TabController1控制器。当您创建第二个模块时,它会覆盖第一个模块,现在只有TabController1,这就是为什么angular抛出错误SelectionCtrl的原因。

有几种方法可以解决此问题。

方法1

创建一个模块并将其存储在某个变量中,并在需要时使用它。

var controllerApp = angular.module('app.newProject', [])
.controller('SelectionCtrl',function(){
    //code here
});

controllerApp.controller('TabController1',function(){
 //your code here
});


方法2

创建一个模块,每当您要使用它时,都应无依赖地使用它。

angular.module('app.newProject', [])
.controller('SelectionCtrl',function(){
    //code here
});

angular.module('app.newProject').controller('TabController1',function(){
 //your code here
});


方法3(我不喜欢这种方法)

创建一个模块,并以线性方式附加组件。

angular.module('app.newProject', [])
.controller('SelectionCtrl',function(){
    //code here
})
.controller('TabController1',function(){
 //your code here
});


我希望您使用方法2,该方法将为您提供通过引用模块来绑定任何组件的方法。

09-30 16:27
查看更多