AngularJS Controller

Controller in AngularJS

As we know angularjs support mvc architecture which stands for model view controller. In mvc view represents the user interface and model is the data.

Both view and model is independent to each other. Controller is like bridge between model and view.

Module in angularjs

In this article before starting the example of controller I want to explain what is module in angularjs. Module in angularjs is just like container which is having the business logics, controllers, services etc. it is just like main. As main contain the startup logic and in angularjs we use module for startup.

To create module, we use angular.module function. In this function we pass first parameter as module name and second parameter is dependent module. If don’t have any dependent module then we can leave it as blank.

var angmodule= angular.module('mymodule',[])

in this code mymodule is module name and empty array represents that there is no dependency on module.

We can use this module with ng-app in the following manner

<html data-ng-app="mymodule">
</html>

Controller in AngularJS

In angularjs controller is javascript function object which works as a mediator between view and model. Controller collects data from Model and pass it to View, i.e html in this case.

$scope in angularjs

$scope is the kind of object which represents the application model. It is used to pass data between view and controller. We can add properties in this scope and we can use it.

Example of Controller

In this example we are creating a controller in which we need to pass data to view i.e. the name of the course along with the fees. So we will write the code in the following manner