This article explains the controller with a sample application and the usage of controller is also discussed here. In my previous article, we saw what is the difference between AngularJS and other JavaScript frameworks. Here are the series of article written on AngularJS,

Controllers

A controller is a set of JavaScript function which is bound to a specified scope. The ng-controller directive, Angular will instantiate the new controller object, and injects the new scope as a dependency. It contains business logic for the view and avoid using to manipulate the DOM.

Figure 1: Controller

Controller Rules

We can use controller to set up the initial state of the scope object and add behavior to that object.

We do not use controller to manipulate DOM. It should contain only business logic and can use data binding and directives for the DOM manipulation.

We do not use controllers to format input but can use angular from controls instead of that.

We do not use filter output but can use angular filters instead of that.

We do not use controllers to share code or state across controllers but can use angular services instead of that.

We do not manage the life-cycle of other components. Creating a Controller

Requires ng-controller directive.

Add controller code to a module.

Name your controller based on functionality.

Controller are named using camel case (i.e. SimpleController).

Setup the initial state of the scope object. When you have create an AngularJS sample application in AngularJs. Firstly, you need to initialize state of the scope object and associate the properties to the scope object in Angular. It is provided in build directive to use ng-controller directive in the AngularJS application.