我正在遵循官方的角度教程,特別是在第4步,我試圖重新組織我的文件,以便與功能相關的代碼位於單獨的模塊中,如https://docs.angularjs.org/tutorial/step_04 。AngularJs:TypeError:無法讀取未定義的屬性'控制器'
當我在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代碼
<!DOCTYPE html>
<html ng-app="carpoolApp">
<head>
<title>Isha Carpool App</title>
</head>
<body>
<header>
</header>
<section>
<greet-user></greet-user> <!-- this works -->
<trip-list></trip-list> <!-- this works only if I define the component in trip-list.module.js. Doesn't work if I put it in a separate file and include it after loading trip-list.module.js as mentioned below -->
</section>
<footer>
</footer>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.js"></script>
<script src="app.module.js"></script>
<script src="trip-list/trip-list.module.js"></script>
<script src="trip-list/trip-list.component.js"></script>
</body>
</html>
- 的Javascript:app.module.js
var carpoolApp = angular.module('carpoolApp', [
'tripList'
]);
carpoolApp.component('greetUser', {
template: 'Hello, {{$ctrl.user}}!',
controller: function GreetUserController() {
this.user = 'world';
}
});
- 使用Javascript - 行程列表/跳閘list.module.js
angular.module('tripList', []);
- 的Javascript:行程列表/跳閘list.component.js
'use strict';
// this prints a valid object
console.log('tripList module in component', angular.module('tripList'));
angular.
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'));
https://plnkr.co/edit/w9odyMbzjzUrryfRCeqh - 版本的作品(該組件的跳閘list.module.js本身內部限定 – Prasanna