I am following the official angular tutorial, specifically at step 4 where I am trying to reorganize my files so that a feature related code is in a separate module as explained in https://docs.angularjs.org/tutorial/step_04.

When I define a component in trip-list/trip-list.module.js it works but if I move the definition to a separate file called trip-list/trip-list.component.js, it throws the following error in both Chrome and 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

Relevant code snippets:

  • Html Code

<!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 - trip-list/trip-list.module.js

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

  • Javascript: trip-list/trip-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'));

share|improve this question
    
plnkr.co/edit/w9odyMbzjzUrryfRCeqh - version that works (the component is defined inside the trip-list.module.js itself. – Prasanna Jan 1 at 20:56
up vote 0 down vote accepted

While trying to send a git PR with a change that worked and that doesn't work, I accidentally made it work. It turns out the problem was that the index.html file didn't have the right EOL (it was in dos format). You can probably see the ^M changes in the diff below. Can't count how many hours I wasted on this!

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>
share|improve this answer

You have to use setter of the angular module tripList as below

'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'));
share|improve this answer
    
the module is being created in the trip-list/trip-list.module.js file, this would overwrite that; I suspect the question is more why that module isn't being attached to. – Claies Jan 1 at 19:52
    
Tried the setter. Doesn't work. Btw, as mentioned before, the same snippet that registers a component, works just fine if I put it in trip-list.module.js. So, am not sure if it has something to do with the code itself. – Prasanna Jan 1 at 19:56
    
can you create a plunker in both the cases! – Aravind Jan 1 at 20:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.