ANGULAR.JS: MODULE AND CONTROLLER
A module is a container for different parts of your app(controllers, services, directives, filters). To create a module, you use the angular object’s module() method.
var myApp = angular.module(‘myModule’, []);
The first parameter in the module method is the name of the module being created. The second parameter specifies the module’s dependencies.
A controller is a javascript function. Builds a model for the view to display. A javascript constructor function creates the controller in angular.
var myController = function($scope) {
$scope.message = ‘AngularJSTutorial';
}$scope is an angular object that gets passed into the controller function by Angular’s framework automatically.
CREATING THE CONTROLLER AND REGISTERING IT WITH MODULE
myApp.controller("myController", function($scope) {
$scope.message = 'angular.js tutorial';
});If controller name is misspelled, an error is raised or binding expressions in view in controller’s scope will not be evaluated. If property name is misspelled, no error is raised. Instead a null or undefined value is returned.
METHOD CHAINING
Creates module, creates controller, and registers controller with module all in one line.
var myApp = angular
.module(“myModule”, [])
.controller(‘myController’, function($scope) {
var employee = {
firstName: ‘ray’,
lastName: ‘man’,
gender: ‘male’
};
$scope.employee = employee;
});