AngularJS未捕获错误:[$ injector:modulerr] - noob here

时间:2017-03-01 20:09:01

标签: javascript angularjs angularjs-injector

我没有使用ngRoute或任何需要注入的Angular服务。我认为,我正在注入我自己的模块和控制器。但仍然在控制台中出现以下错误:

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)

代码: 的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:

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

博客-list.module.js

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

博客-list.component.js

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

2 个答案:

答案 0 :(得分:1)

问题在这里BlogListController作为一种堕落,

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

将其更改为,

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

从依赖项中删除BlogListController

答案 1 :(得分:1)

这是因为你的HTML没有使用'blogList'模块。将其更改为:

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

同样,在声明模块时,不要注入'BlogListController'。该阵列用于依赖注入新创建的模块运行所需的外部模块

(例如:ngRoute是一个流行的模块,不会出现'开箱即用',必须通过依赖注入包含在内)