AngularJS Concepts
Dear reader ,
I am going to light on some AngularJS concepts and features .
So please read and provide your suggestions if required.
What
is AngularJS?
Ø AngularJS
is an awesome Javascript framework that can be used to create powerful and dynamic web apps. It also
covers the building of complex client-side applications.
Since its release in 2009, AngularJS has been widely used by many developers
for its convenient extending of custom HTML tags and attributes, known as
‘directives’.
Ø AngularJS is an
open source web application framework. It was originally developed in 2009 by
Misko Hevery and Adam Abrons. It is now maintained by Google. Its latest
version is 1.3.14.
Features
·
AngularJS is a powerful
JavaScript based development framework to create RICH Internet
Application(RIA).
·
AngulajJS provides
developers options to write client side application (using JavaScript) in a
clean MVC(Model View Controller) way.
·
Application written in
AngularJS is cross-browser compliant. AngularJS automatically handles
javascript code suitable for each browser.
·
AngularJS is open
source, completely free, and used by thousands of developers around the world.
It is licensed under the Apache License version 2.0.
·
It supports two way
binding.
Overall, AngularJS is a
framework to build large scale and high performance web appliation while
keeping them as easy-to-maintain.
AngularJS is a MVC based framework. In the coming
chapters, we will see how AngularJS uses MVC methodology.
Core Features
Following are most important core
features of AngularJS:
·
Data-binding: It is the automatic synchronization of data
between model and view components. Supports Two way binding.
·
Scope: These are objects that refer to the model.
They act as a glue between controller and view.
·
Controller: These are Javascript functions that are bound
to a particular scope.
·
Services: AngularJS come with several built-in services
for example $http to make a XMLHttpRequests. These are singleton objects which
are instantiated only once in app.
·
Filters: These select a subset of items from an array
and returns a new array.
·
Directives: Directives are markers on DOM elements (such
as elements, attributes, css, and more). These can be used to create custom
HTML tags that serve as new, custom widgets. AngularJS has built-in directives
(ngBind, ngModel...)
·
Templates:These are the rendered
view with information from the controller and model. These can be a single file
(like index.html) or multiple views in one page using "partials".
·
Routing: It is concept of switching views.
·
Model View Whatever: MVC is a design pattern for dividing an
application into different parts (called Model, View and Controller), each with
distinct responsibilities. AngularJS does not implement MVC in the traditional
sense, but rather something closer to MVVM (Model-View-ViewModel). The Angular
JS team refers it humorously as Model View Whatever.
·
Deep Linking: Deep linking allows you to encode the state
of application in the URL so that it can be bookmarked. The application can
then be restored from the URL to the same state.
·
Dependency Injection: AngularJS has a built-in dependency injection
subsystem that helps the developer by making the application easier to develop,
understand, and test.
Concepts
Following diagram depicts some
important parts of AngularJS which we will discuss in detail in the subsequent
chapters.
Advantages of AngularJS
·
AngularJS provides capability to create Single Page Application in
a very clean and maintainable way.
·
AngularJS provides data binding capability to HTML thus giving
user a rich and responsive experience
·
AngularJS code is unit testable.
·
AngularJS uses dependency injection and make use of separation of
concerns.
·
AngularJS provides reusable components.
·
With AngularJS, developer write less code and get more
functionality.
·
In AngularJS, views are pure html pages, and controllers written
in javascript do the business processing.
On top of everything, AngularJS
applications can run on all major browsers and smart phones including Android
and iOS based phones/tablets.
Disadvantages of
AngulaJS
Though AngularJS comes with lots
of plus points but same time we should consider the following points:
·
Not Secure : Being JavaScript only framework,
application written in AngularJS are not safe. Server side authentication and
authorization is must to keep an application secure.
·
Not degradable: If your
application user disables Javascript then user will just see the basic page and
nothing more.
The AngularJS
Components
The AngularJS framework can be
divided into following three major parts:
·
ng-app : This directive defines and links an
AngularJS application to HTML.
·
ng-model : This directive binds the values of
AngularJS application data to HTML input controls.
·
ng-bind : This directive binds the AngularJS
Application data to HTML tags.
Example
Now let us write a simple example
using AngularJS library. Let us create an HTML filemyfirstexample.html as below:
<!doctype html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.17/angular.min.js"></script>
</head>
<body ng-app="myapp">
<div ng-controller="HelloController" >
<h2>Welcome {{helloTo.title}} to the world of Tutorialspoint!</h2>
</div>
<script>
angular.module("myapp", [])
.controller("HelloController", function($scope) {
$scope.helloTo = {};
$scope.helloTo.title = "AngularJS";
});
</script>
</body></html>
Following sections describe the
above code in detail:
Include AngularJS
We have included the AngularJS
JavaScript file in the HTML page so we can use AngularJS:
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</head>
Check the latest version of
AngularJS on their official website.
Point to AngularJS app
Next we tell what part of the
HTML contains the AngularJS app. This done by adding the ng-app attribute to the root HTML element of the
AngularJS app. You can either add it to htmlelement or body element as shown below:
<body ng-app="myapp">
</body>
View
The view is this part:
<div ng-controller="HelloController" >
<h2>Welcome {{helloTo.title}} to the world of Tutorialspoint!</h2>
</div>
ng-controller tells AngularJS what controller to use with
this view. helloTo.title tells AngularJS to write the
"model" value named helloTo.title to the HTML at this location.
Controller
The controller part is:
<script>
angular.module("myapp", [])
.controller("HelloController", function($scope) {
$scope.helloTo = {};
$scope.helloTo.title = "AngularJS";
});
</script>
This code registers a controller
function named HelloController in the angular module named myapp. We will study more about modules and controllers in their respective chapters. The controller
function is registered in angular via the angular.module(...).controller(...)
function call.
The $scope parameter passed to
the controller function is the model. The
controller function adds a helloTo JavaScript object, and in that object it adds
a title field.
Execution
Save the above code as myfirstexample.html and open it in any browser. You will see an
output as below:
When the page is loaded in the
browser, following things happen:
·
HTML document is loaded into the browser, and evaluated by the
browser. AngularJS JavaScript file is loaded, the angular global object is created. Next, JavaScript which
registers controller functions is executed.
·
Next AngularJS scans through the HTML to look for AngularJS apps
and views. Once view is located, it connects that view to the corresponding
controller function.
·
Next, AngularJS executes the controller functions. It then renders
the views with data from the model populated by the controller. The page is now
ready.
AngularJS First Application
Before we start with creating
actual HelloWorld application using AngularJS, let us see what are the actual
parts of a AngularJS application. An AngularJS application consists of
following three important parts
·
ng-app : This directive defines and links an
AngularJS application to HTML.
·
ng-model : This directive binds the values of
AngularJS application data to HTML input controls.
·
ng-bind : This directive bidns the AngularJS
Application data to HTML tags.
Steps to create
AngularJS Application
Step 1: Load framework
Being a pure JavaScript framework,
It can be added using <Script> tag.
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">
</script>
Step 2: Define AngularJS Application using
ng-app directive
<div ng-app="">
...
</div>
Step 3: Define a model name using ng-model
directive
<p>Enter your Name: <input type="text" ng-model="name"></p>
Step 3: Bind the value of above model defined
using ng-bind directive.
<p>Hello <span ng-bind="name"></span>!</p>
Steps to run AngularJS
Application
Use above mentioned three stpes
in an HTML page.
testAngularJS.htm
<html>
<title>AngularJS First Application</title>
<body>
<h1>Sample Application</h1>
<div ng-app="">
<p>Enter your Name: <input type="text" ng-model="name"></p>
<p>Hello <span ng-bind="name"></span>!</p> or <h1>Hello {{name}}!</h1>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</body>
</html>
Output
Open textAngularJS.htm in a web
browser. Enter your name and see the result.
How AngularJS
integrates with HTML
·
ng-app directive indicates the start of AngularJS application.
·
ng-model directive then creates a model variable named
"name" which can be used with the html page and within the div having
ng-app directive.
·
ng-bind then uses the name model to be displayed in the html span
tag whenever user input something in the text box.
·
Closing</div> tag indicates the end of AngularJS
application.
AngularJS directives are used to
extend HTML. These are special attributes starting with ng- prefix. We're going
to discuss following directives:
·
ng-app - This directive starts an AngularJS
Application.
·
ng-init - This directive initializes application
data.
·
ng-model - This directive defines the model that is
variable to be used in AngularJS.
·
ng-repeat - This directive repeats html elements for
each item in a collection.
·
ng-app
directive
·
ng-app directive starts
an AngularJS Application. It defines the root element. It automatically
initializes or bootstraps the application when web page containing AngularJS
Application is loaded. It is also used to load various AngularJS modules in
AngularJS Application. In following example, we've defined a default AngularJS
application using ng-app attribute of a div element.
·
<div ng-app="">
·
...
·
</div>
·
ng-init
directive
·
ng-init directive
initializes an AngularJS Application data. It is used to put values to the
variables to be used in the application. In following example, we'll initialize
an array of countries. We're using JSON syntax to define array of countries.
·
<div ng-app="" ng-init="countries=[{locale:'en-US',name:'United
States'},
·
{locale:'en-GB',name:'United
Kingdom'},
·
{locale:'en-FR',name:'France'}]">
·
·
...
·
</div>
·
ng-model
directive
·
ng-model directive
defines the model/variable to be used in AngularJS Application. In following
example, we've defined a model named "name".
·
<div ng-app="">
·
...
·
<p>Enter your Name: <input type="text" ng-model="name"></p>
·
</div>
·
ng-repeat
directive
·
ng-repeat directive
repeats html elements for each item in a collection.. In following example,
we've iterated over array of countries.
·
<div ng-app="">
·
...
·
<p>List of Countries with locale:</p>
·
<ol>
·
<li ng-repeat="country in
countries">
·
{{ 'Country: ' + country.name + ',
Locale: ' + country.locale }}
·
</li>
·
</ol>
·
</div>
·
Example
·
Following example will
showcase all the above mentioned directives.
·
testAngularJS.htm
·
<html>
·
<title>AngularJS Directives</title>
·
<body>
·
<h1>Sample Application</h1>
·
<div ng-app="" ng-init="countries=[{locale:'en-US',name:'United
States'},
·
{locale:'en-GB',name:'United
Kingdom'},
·
{locale:'en-FR',name:'France'}]">
·
<p>Enter your Name: <input type="text" ng-model="name"></p>
·
<p>Hello <span ng-bind="name"></span>!</p>
·
<p>List of Countries with locale:</p>
·
<ol>
·
<li ng-repeat="country in
countries">
·
{{ 'Country: ' + country.name + ',
Locale: ' + country.locale }}
·
</li>
·
</ol>
·
</div>
·
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
·
</body>
·
</html>
Output
Open textAngularJS.htm in a web
browser. Enter your name and see the result.
Reference from :
http://www.tutorialspoint.com/angularjs/angularjs_overview.htm