I have some simple Angular.js code that I do not understand.
HTML:
<div ng-controller="MyCtrl">
Hello, {{name}}!
</div>
JavaScript:
var myApp = angular.module('myApp',[]);
var controller = myApp.controller('MyCtrl', ['$scope', function ($scope) {
$scope.name = 'Superhero';
}]);
alert(controller.name);
The HTML directive produces what I would expect: "Hello, Superhero!" is rendered to the page. But the alert box is perplexing. I would expect it to show the string "Superhero", or perhaps the controller's name "MyCtrl", but what it actually shows is the module's name "MyApp". Why? What would I need to pass to alert to show the name assigned to the controller's scope (i.e. "Superhero") or the name of the controller itself (i.e. "MyCtrl")?
(N.B. I did try the documentation but it didn't help me untangle why the controller is reporting the module's name instead of its own.)
myApp.controller()returns the module so you can chain registration together