1. Declare as global function |
function Ctrl($scope){
//controller logic
} |
- |
It is simple JavaScript function containing $scope as an argument
Not supported in recent versions (1.4 onwards) |
2. Declaring controller on entire app, i.e., the main module of the application |
Var app = angular.module("app",[]);
app.controller(‘Ctrl’, function($scope){
//Controller logic
}); |
<html ng-app="app"> |
The main module is loaded automatically when the application bootstraps using ng-app directive.
The visibility of the controller is to the module app |
3. Declaring controller in a module within the main module |
var app=angular.module("app", [‘controllers’]);
var controllers=angular.module("controllers",[]);
controllers.controller(‘Ctrl’, function($scope){
//controller logic
}); |
<html ng-app="app"> |
The main module is loaded automatically when the application bootstraps using ng-app directive
The visibility of the controller is to the module ‘controllers’ and visibility of module controllers is to the module ‘app’ |