Javascript AngularJs:TypeError:无法读取属性';控制器';未定义的

Javascript AngularJs:TypeError:无法读取属性';控制器';未定义的,javascript,angularjs,Javascript,Angularjs,我遵循官方的angular教程,特别是在步骤4中,我尝试重新组织我的文件,以便与功能相关的代码位于单独的模块中,如中所述 当我在trip list/trip-list.module.js中定义一个组件时,它可以工作,但是如果我将该定义移动到一个名为trip list/trip-list.component.js的单独文件中,它会在Chrome和IE 9中抛出以下错误 angular.js:68 Uncaught Error: [$injector:modulerr] Failed to inst

我遵循官方的angular教程,特别是在步骤4中,我尝试重新组织我的文件,以便与功能相关的代码位于单独的模块中,如中所述

当我在trip list/trip-list.module.js中定义一个组件时,它可以工作,但是如果我将该定义移动到一个名为trip list/trip-list.component.js的单独文件中,它会在Chrome和IE 9中抛出以下错误

angular.js:68 Uncaught Error: [$injector:modulerr] Failed to instantiate module carpoolApp due to: 
Error: [$injector:modulerr] Failed to instantiate module tripList due to: 
TypeError: Cannot read property 'controller' of undefined
相关代码片段:

  • Html代码

伊莎拼车应用程序

您必须使用角度模块的setter三列表,如下所示

'use strict';

// this prints a valid object
console.log('tripList module in component', angular.module('tripList'));

angular.
  //modified the below line
  module('tripList',[ ]).
  component('tripList', {
    templateUrl: 'trip-list/trip-list.template.html',
    controller: function TripListController() {
        this.trips = [
            {
                from: 'BNA',
                to: 'iii',
                departureDateAndTime: new Date()
            },
            {
                from: 'iii',
                to: 'BNA',
                departureDateAndTime: new Date()
            }
        ];
    }
});

// this too prints a valid object
console.log("tripList component registered", angular.module('tripList').component('tripList'));

当我试图发送一个git PR,其中包含一个有效的更改和一个无效的更改时,我意外地使它起了作用。原来问题是index.html文件没有正确的EOL(它是dos格式的)。您可能可以在下面的差异中看到^M更改。数不清我在这上面浪费了多少时间

diff --git a/src/main/webapp/index.html b/src/main/webapp/index.html
index fd50422..86d0666 100644
--- a/src/main/webapp/index.html
+++ b/src/main/webapp/index.html
@@ -23,8 +23,9 @@
 <script src="trip-list/trip-list.module.js"></script>
 <!--
 TODO: This doesn't work :(
-<script src="trip-list/trip-list.component.js"></script>
 -->
+<script src="trip-list/trip-list.component.js"></script>^M
+^M

</body>
</html>
diff--git a/src/main/webapp/index.html b/src/main/webapp/index.html
索引fd50422..86d0666 100644
---a/src/main/webapp/index.html
+++b/src/main/webapp/index.html
@@ -23,8 +23,9 @@
+^M
+^M

正在
行程列表/trip list.module.js
文件中创建模块,这将覆盖该模块;我怀疑问题更多的是为什么没有连接到该模块。尝试了setter。不起作用。顺便说一句,如前所述,注册组件的相同代码段,如果我将其放在trip-list.module.js中,就可以正常工作。因此,我不确定它是否与代码本身有关。您能在这两种情况下创建一个plunker吗!-有效版本(组件在trip-list.module.js本身中定义)。