In the last article, I showed you how to set up your page to use Angular and do simple, one-way data binding.
In this article, I will describe modules and controllers.
A module is a container for the parts of an Angular application, including its controllers. This helps provide a separation of concerns, which helps us organize large, complex applications.
In the example above, we named the controller "mainController", but we can give it any name we want. The function argument contains the controller's code and we can pass into this function any Angular objects that we want the function to use.
One common parameter to pass to a controller function is $scope. $scope is a built-in object designed to hold stateful data. We can attach properties to $scope and they will be available in both the view (the web page) and the controller.
The ng-controller directive is an attribute that identifies which controller is available to a page or a section of a page. We can add this attribute to any element, but the controller is only available to that element and the objects contained inside it. If we add it to the body tag, it is available to anything within the body, as in the following example:
which points to the MyController controller and makes it available to everything in the page body.
In the above example, we use the one-way data binding to display the properties of $scope set within the controller. The output of this is:
This is a simple example, but you can do what you want in a controller and anything created or manipulated in that function is availabe to your web page.
In this article, we introduced Angular controllers and showed how to use them in an Angular application.