Rodolfo A. Calvo Jaubert Rodolfo A. Calvo Jaubert - 6 months ago 28
AngularJS Question

Angular error in MEAN stack - Uncaught Error: $injector:modulerr

I am making a simple in MEAN stack, the code starts fine at the beginning, when I code a couple of lines more Angular shows me an error. What I am doing wrong? I cannot follow with the Internet MEAN stack courses for this issue.
PLease help with this issue.


Html Code of index.


<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Contact list App</title>
<link rel="stylesheet" href="css/metro-bootstrap.min.css" />
</head>
<body>
<div class="container" ng-controller="AppCtrl">
<section class="row">
<div class="col-lg-12 text-center">
<h1>This is a MEAN stack - Contact list!</h1>
<table class="table">
<thead>
<tr>
<th class="text-center">Name</th>
<th class="text-center">Email</th>
<th class="text-center">P. Number</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="contact in contactist">
<td>{{contact.name}}</td>
<td>{{contact.email}}</td>
<td>{{contact.number}}</td>
</tr>
</tbody>
</table>
</div>
</section>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/metro-docs.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.12/angular.min.js"></script>
<script type="text/javascript" src="controllers/controller.js"></script>
</body>





Controller


var myApp = angular.module('myApp', ['ngRoute']);function AppCtrl($scope, $http) {
console.log("Hello world from controller.js");

$http.get('/contactlist').success(function (response) {
console.log("I got the data");
$scope.contactList = response;
});}



Server


var express = require('express');
var app = express();
app.use(express.static(__dirname + "/public"));

app.get('/contactList', function (req, res) {
console.log("I got the request!");

person1 = {
name: 'Tim',
email: 'tim@domain.com',
number: '(111) 11-11-111'
};
person2 = {
name: 'Rod',
email: 'rod@domain.com',
number: '(211) 11-11-111'
};
person3 = {
name: 'Eddie',
email: 'eddie@domain.com',
number: '(311) 11-11-111'
};

res.json([person1, person2, person3]);
});

app.listen(3000);
console.log("Server running from port 3000");



This is the error: Uncaught Error: [$injector:modulerr]
[AngularJs Error]1


enter image description here

Answer

It looks as though your controller is not wired up - you've declared the function, bit not told angular about it. Try this:

var myApp = angular.module('myApp', ['ngRoute'])

myApp.controller('AppCtrl', function($scope, $http) {
    console.log("Hello world from controller.js");

    $http.get('/contactlist').success(function (response) {
      console.log("I got the data");
      $scope.contactList = response;
   });
});

Also I see no sign of the script tag loading angular, but it must be there to get the error message. Have you remembered also to include ngRoute, with e.g.?

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.6/angular-route.js"></script>