简体   繁体   English

在angularjs中加载多个模块

[英]loading multiple modules in angularjs

I'm constantly getting this error - 我不断收到此错误-

https://docs.angularjs.org/error/ $injector/nomod?p0=interactive-main https://docs.angularjs.org/error/ $ injector / nomod?p0 = interactive-main

The project structure (with multiple people developing and all new to angularjs) looks like this - 项目结构(由多人开发,并且对angularjs来说都是新的)看起来像这样-

public
   |--css, font-awesome
   |--js (where angular.min.js and other jquery libraries are)
   |--views (where html for each pages are)
        |--myHTMLcontents
        |--js
index.html

The index.html has it's own data-ng-app name and it's used in the app.js file. index.html具有其自己的data-ng-app名称,并在app.js文件中使用。 I have my HTML files in the views folder, and the corresponding js files are in the views folder also. 我的HTML文件位于views文件夹中,并且相应的js文件也位于views文件夹中。 I tried to create with 我尝试用

angular.module('myApp').controller(....

But I guess you're supposed to only create angular.module once (which is in the app.js), but I'm still unsure how to add 'myApp' in app.js. 但是我想您应该只创建一次angular.module(在app.js中),但是我仍然不确定如何在app.js中添加“ myApp”。

I tried to do this: 我试图这样做:

(function () {
    angular.module('data-ng-app Name', [
        'ui.router',                    // Routing
        'oc.lazyLoad',                  // ocLazyLoad
        'ui.bootstrap',                 // Ui Bootstrap
        'myApp'
    ])
})();

The nodejs server doesn't throw error, but nothing shows on the page (after I added 'myApp'). nodejs服务器不会引发错误,但是页面上没有任何显示(添加了“ myApp”之后)。 How do you solve this? 您如何解决呢?

You need to create the module 'myApp' once and run it in app.js. 您需要一次创建模块“ myApp”,然后在app.js中运行它。 below is an example. 下面是一个例子。

angular.module('myApp', []).run()

And then in your controller 然后在您的控制器中

angular.module('myApp.controllers', []).controller('RegisterCtrl', function () {});

First make sure your declare the myapp module file in your index before inject it into an other one : 首先,请确保在索引中声明myapp模块文件,然后再将其注入另一个文件:

<script src="js/myapp.js"></script>
<script src="js/my-module-injecting-myapp.js"></script>

Then two ways : The first one is directly calling the module, by the way you did : 然后有两种方式:第一种是通过您的方式直接调用模块:

angular.module('myApp').controller(....

The second and what I used to do is declaring it as variable: 第二个和我以前所做的就是将其声明为变量:

var myAppModule = angular.module('myApp', []);

Then to declare a controller, service , whatever, just call the variable: 然后要声明一个控制器service,只要调用变量即可:

myAppModule.controller("myAppController", function(){...

One last thing, I don't know if was an example but do not declare your module name with space : 最后一件事,我不知道这是否是一个示例,但不要用空格声明模块名称:

angular.module('data-ng-app Name', [....

should be : 应该 :

angular.module('myModuleNamewithoutSpace', [....

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

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