Controllers are one of the most important building blocks of an AngularJS application, and they act as a channel between the model and the views. When we are developing a big Angular JS Application, we have to works with multiple controllers, each of which delivers the data and logic required for one aspect of the application.
We can define a Controllers are using the Module.controller method, which takes two arguments: the name of the
controller and a factory function, which is used to set up the controller and get it ready for use. Following statements that create the controller.
...
myApp.controller("dayCtrl", function ($scope) {
// controller statements will go here
});
...
The commonly used convention for controller names is to use the suffix Ctrl. Above statement will creates a new
controller called dayCtrl. The function passed to the Module.controller method is used to declare the controller’s
dependencies, which are the AngularJS components that the controller requires. AngularJS provides some built-in
services and features that are specified using argument names that start with the $ symbol. In the above code you can see that we have specified the $scope, which asks AngularJS to provide the scope for the controller. To declare a dependency on $scope, We just have to use the name as an argument to the factory function. E.g :
...
myApp.controller("dayCtrl", function ($scope) {
...
This is an example of dependency injection (DI), where AngularJS inspects the arguments that are specified for a
function and locates the components they correspond to; It is suggested to read “Understanding Dependency Injection in Angular JS” article for more details. The function we passed to the controller method has an argument called x, and AngularJS will automatically pass in the scope object when the function is called. You can also read dependency injection Tutorials in order to understand in details.
As this article is also a part of Concepts while working with Angular JS Application . So, you must read other tutorials in order to complete understanding to developing Angular JS Application. Since, we are going to learn several concepts. That's why I have broken this article into several small article. Which allow us to easily understand several Angular JS features and concepts. Following are the list of articles.
Summary: You should read above mentioned articles in order to completely understanding to developing an Angular JS application. This article is a part of Concepts while working with Angular JS Application, which is divided in to pieces of tutorials in order to understand easily. I hope you will like this article and find it useful.