AngularJS: Register Controller from separate .js file

Jade L. picture Jade L. · Sep 30, 2015 · Viewed 7.5k times · Source

I am new to AngularJS and using plnkr.co to learn how to use angular. I have having some difficulty registering a Controller this is stored in a separate .js from where the module is initialized. Can anybody tell me why the below does not work?

index.html file

app.js File

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

mainCtrl.js File

angular.module('plunker')
  .controller('MainCtrl', ['$scope', function($scope) {
    $scope.name = 'GitHub';
  }]);

For me, this produces an Argument 'MainCtrl' is not a function, got undefined.

Answer

Michael P. Bazos picture Michael P. Bazos · Sep 30, 2015

Replace

<script scr="mainCtrl.js"></script>

with

<script src="mainCtrl.js"></script>

Because of the typo (scr instead of src), the source file holding MainCtrl is not loaded, therefore MainCtrl is undefined.