engr_ejo engr_ejo - 4 years ago 279
AngularJS Question

The controller with the name 'mainController' is not registered

i have this code in my script.js file

var mainController = function($scope){
$scope.message = "Plunker";

and this is my HTML

<!DOCTYPE html>
<html ng-app>
<script data-require="angular.js@1.6.1" data-semver="1.6.1" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>

<body ng-controller="mainController">
<h1>Hello {{ message }}</h1>

i declared ng-app in the opening html tag

but i get this error on my console that mainController is not registered

Answer Source

To paraphrase http://www.w3schools.com/angular/angular_modules.asp

 <div ng-app="myApp" ng-controller="mainController">
{{ firstName + " " + lastName }}


var app = angular.module("myApp", []);

app.controller("mainController", function($scope) {
    $scope.firstName = "John";
    $scope.lastName = "Doe";


The important line is

    app.controller("mainController", function($scope) 

which injects your controller into your app

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download