Professional Documents
Culture Documents
(This is the third post in my series on the AngularJS; check out my initial piece covering
‘An intro to AngularJS’ and ‘Data-binding methods in AngularJS’.)
Before having a look at AngularJS’ components, terms and terminologies, let’s make sure
everyone is on the same page. If not then you can go through our previous blogs “Introduction to
AngularJs” and “Data-binding methods in AngularJS” to have a clear understanding of binding
technique and when/where these can be used for a better result.
When we talk about AngularJS, we come across many new terms and terminologies such as
Angular module controller, and others. So why these modules and controllers are called building
blocks of an Angular application. And what are the use cases.
First thing first, let’s start with the main objective of this post. Do not worry when you will complete
this blog, you will have an intermediate level of expertise on AngularJS modules & controllers.
In the previous AngularJS Tutorial, titled Data-binding in AngularJS, you might have noticed the
source code similar to the one mentioned below:
Parameter
name: name of the module.
requires: is an optional parameter, if mentioned the new module get created. If not, an existing
module is being retrieved.
configFn: is an optional parameter, execute function on module load
The universal way to create or register the module with Angular is Parameter. It has a suite of
services, controllers, services, configuration & directives. In other words, you can consider it as a
main() function to initialize and get chained with variable and methods for an application.
Sample code:
Remember, there could be a scenario when your custom module will be dependent on another
module. In that situation, you can pass that module over Angular module function as a parameter
and utilize their resources.
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
In above source code, the <div ng-app =”myApp "> section will start bootstrapping
(initializing/ resource gathering) process. It tells the browser that this is an Angular app. Here you
can also see <p> tag have {{firstname}}. Browser will look for Angular variable and print the value
in the view.
Now let’s move to the controller (new app declaration). A controller is nothing but a JavaScript
constructor function, which is responsible for building a model (data) so that the same data could
be displayed in a view. It has the ability to perform some operation or initializing the variable or
getting the data from endpoint URL.
In other words, when you work on Decouple website, the job of a controller is to get the data from
web services. Hope this information is clear and helpful. Let’s view the basic syntax code and
explanation of code.
Syntax:
var ControllerName = function ($scope) {
/* variable initialization goes here */
}
Explanation: In the above source code, we are creating an anonymous function and assigning
back to a variable. Note that it has one parameter, called $scope, which is an angular object pass
to the controller. ControllerName is the name of the controller. Using the AngularJS Data binding
methods, we can fetch the data in the view. Here we are also assigning ValueA property to
$scope object.
Once controller has been created. The next step would be to Register the controller with the
newly created module (myApp).
Syntax:
Sample:
Myapp.controller(“'myCtrl'”, function($scope) {
$scope.firstname = "Jaywant Topno";
});
Follow the above steps, to:
or
In case the name of controller gets changed then you need to make same changes in the view
also. Else view won’t be able to recognize your variable.
Sample code:
So far we have created a module, a controller and registered that newly created controller.
Now let’s have a look how to pull up model to view using ng-app and ng-controller.
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app ="NewApp">
<script src="https://gist.github.com/xaiwant/8f363eea536546cf33757f8319e597b6.js"></script>
Points to remember:
1. View data won’t work outside scope until the controller is declared in self <div> directory.
2. Create and register controller within single code to reduce unwanted memory allocation.
Phew! That was quite a ride. Remember, before building an application, it is necessary to have an
intermediate level knowledge of AngularJS. Modules and controllers are building blocks of a newly
created module where controller helps to initialize variable and perform an operation. Further,
using ng-app we can tell the view to use specific app and ng-controller for scope of the variable.