简体   繁体   English

AngularJS未被捕获的错误:[$ injector:modulerr] —菜鸟在这里

[英]AngularJS Uncaught Error: [$injector:modulerr] — noob here

I'm not using ngRoute or any Angular service that needs to be injected. 我没有使用ngRoute或任何需要注入的Angular服务。 And I'm injecting my own module and controller as, I think, needed. 我认为我需要注入自己的模块和控制器。 But still getting the following error in the console: 但是在控制台中仍然出现以下错误:

Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.5.7/$injector/modulerr?p0=try&p1=Error%3A%20%…2015%2520experiments%2Fangulartrial%2Fjs%2Flib%2Fangular.min.js%3A21%3A163)
at angular.min.js:6
at angular.min.js:40
at r (angular.min.js:7)
at g (angular.min.js:39)
at db (angular.min.js:43)
at c (angular.min.js:20)
at Bc (angular.min.js:21)
at ge (angular.min.js:19)
at angular.min.js:315
at HTMLDocument.b (angular.min.js:189)

Code: Index.html: 代码:Index.html:

<html ng-app="try" lang="En">
<head>
    <title>Learn Angular 1.5</title>
    <!-- <script src="./js/lib/jquery-3.1.0.min.js" charset="utf-8"></script> -->
    <script src='./js/lib/angular.min.js'></script>
    <script src='app.js'></script>
    <script src='./js/app/blog-list.module.js'></script>
    <script src='./js/app/blog-list.component.js'></script>

</head>
<body>
    <div class='' ng-controller = 'BlogListController'>
    </div>
</body>
</html>

app.js: app.js:

angular.module ('try', ['blogList'])
    //This works when I declare the controller right here
    // .controller('BlogListController', function(){
    //  console.log("Hello");
    // })

blog-list.module.js 博客,list.module.js

'use strict';
//simply declare the module here 
angular.module('blogList', [
    //inject dependencies
    'BlogListController'
    ]);

blog-list.component.js 博客,list.component.js

 //declare the controllers, components etc on the module here 
angular.module('blogList')
    .controller('BlogListController', function(){
        console.log("Hello");
    });

Issue is here BlogListController as a depdendency, 这里的问题是BlogListController作为依赖关系,

angular.module('blogList', [
    //inject dependencies
    'BlogListController'
    ]);

Change it as, 更改为

angular.module('blogList', []);

Remove the BlogListController from the dependencies 从依赖项中删除BlogListController

It's because you're HTML is not using the 'blogList' module. 这是因为您使用的HTML没有使用“ blogList”模块。 Change it to: 更改为:

<html ng-app="blogList" lang="En">
 ...
</html>

Also you don't inject your 'BlogListController' when declaring the module. 另外,在声明模块时,您不会注入“ BlogListController”。 The array is meant for dependency injection of external modules that your newly created module would require to run 该数组用于依赖注入新创建的模块将需要运行的外部模块

(ex: ngRoute is a popular module that doesn't come 'out of box' and has to be included via dependency injection) (例如:ngRoute是一个流行的模块,不会“开箱即用”,必须通过依赖注入将其包含在内)

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

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