Angularjs Tutorial by Hopeseller
Angularjs Tutorial by Hopeseller
OVERVIEW
AngularJS is a structural framework for dynamic web applications. It lets you use
HTML as your template language and lets you extend HTML's syntax to express
your application components clearly and succinctly. Its data binding and
dependency injection eliminate much of the code you currently have to write. And
it all happens within the browser, making it an ideal partner with any server
technology.
It was originally developed in 2009 by Misko Hevery and Adam Abrons. It is now
maintained by Google.
General Features
The most important general features of AngularJS are:
AngularJS is a efficient framework that can create Rich Internet Applications
(RIA).
AngularJS provides developers an options to write client side applications
using JavaScript in a clean Model View Controller (MVC) way.
Applications written in AngularJS are 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.
Core Features
The most important core features of AngularJS are:
1
Angular JS Tutorial
Concepts
The following diagram depicts some important parts of AngularJS which we will
discuss in detail in the subsequent chapters.
2
Angular JS Tutorial
Advantages of AngularJS
The advantages of AngularJS are:
3
Angular JS Tutorial
On the 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 a lot of merits, here are some points of concern:
AngularJS Directives
The AngularJS framework can be divided into three major parts:
4
2. ENVIRONMENT Angular JS Tutorial
This chapter describes how to set up AngularJS library to be used in web application
development. It also briefly describes the directory structure and its contents.
When you open the link https://angularjs.org/, you will see there are two options to
download AngularJS library:
View on GitHub- By clicking on this button, you are diverted to GitHub and
get all the latest scripts.
Download- By clicking on this button, a screen you get to see a dialog box
shown as:
5
Angular JS Tutorial
We are using the CDN versions of the library throughout this tutorial.
Example
Now let us write a simple example using AngularJS library. Let us create an HTML
filemyfirstexample.html shown as below:
<!doctype html>
<html>
6
Angular JS Tutorial
<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>
<script>
angular.module("myapp", [])
.controller("HelloController", function($scope) {
$scope.helloTo = {};
$scope.helloTo.title = "AngularJS";
});
</script>
</body>
</html>
Let us go through the above code in detail:
Include AngularJS
We include the AngularJS JavaScript file in the HTML page so that we can use it:
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js">
</script>
</head>
You can check the latest version of AngularJS on its official website.
7
Angular JS Tutorial
<body ng-app="myapp">
</body>
View
The view is this part:
</div>
ng-controller tells AngularJS which controller to use with this view. helloTo.title
tells AngularJS to write the model value named helloTo.title in HTML at this
location.
Controller
The controller part is:
<script>
angular.module("myapp", [])
.controller("HelloController", function($scope) {
$scope.helloTo = {};
$scope.helloTo.title = "AngularJS";
});
</script>
The $scope parameter model is passed to the controller function. The controller
function adds a helloTo JavaScript object, and in that object it adds a title field.
8
Angular JS Tutorial
Execution
Save the above code as myfirstexample.html and open it in any browser. You get
to see the following output:
What happens when the page is loaded in the browser ? Let us see:
HTML document is loaded into the browser, and evaluated by the browser.
AngularJS JavaScript file is loaded, the angular global object is created.
The JavaScript which registers controller functions is executed.
Next, AngularJS scans through the HTML to search for AngularJS apps as
well as views.
Once the 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.
9
3. MVC ARCHITECTURE Angular JS Tutorial
Model - It is the lowest level of the pattern responsible for maintaining data.
View - It is responsible for displaying all or a portion of the data to the user.
Controller - It is a software Code that controls the interactions between the
Model and View.
MVC is popular as it isolates the application logic from the user interface layer and
supports separation of concerns. The controller receives all requests for the
application and then works with the model to prepare any data needed by the
view. The view then uses the data prepared by the controller to generate a final
presentable response. The MVC abstraction can be graphically represented as
follows.
10
Angular JS Tutorial
The model
The model is responsible for managing application data. It responds to the request
from view and to the instructions from controller to update itself.
The view
A presentation of data in a particular format, triggered by the controller's decision
to present the data. They are script-based template systems such as JSP, ASP, PHP
and very easy to integrate with AJAX technology.
The controller
The controller responds to user input and performs interactions on the data model
objects. The controller receives input, validates it, and then performs business
operations that modify the state of the data model.
AngularJS is a MVC based framework. In the coming chapters, let us see how
AngularJS uses MVC methodology.
11
4. FIRST APPLICATION Angular JS Tutorial
Before creating actual Hello World ! application using AngularJS, let us see the
parts of a AngularJS application. An AngularJS application consists of following
three important parts:
<script
src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js">
</script>
<div ng-app="">
...
</div>
12
Angular JS Tutorial
testAngularJS.htm
<html>
<body>
<h1>Sample Application</h1>
<div ng-app="">
</div>
<script
src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js">
</script>
</body>
</html>
Output
Open the file testAngularJS.htm in a web browser. Enter your name and see the
result.
13
Angular JS Tutorial
14
5. DIRECTIVES Angular JS Tutorial
AngularJS directives are used to extend HTML. They are special attributes starting
with ng-prefix. Let us discuss the following directives:
ng-app directive
The ng-app directive starts an AngularJS Application. It defines the root element. It
automatically initializes or bootstraps the application when the web page
containing AngularJS Application is loaded. It is also used to load various AngularJS
modules in AngularJS Application. In the following example, we define a default
AngularJS application using ng-app attribute of a <div> element.
<div ng-app="">
...
</div>
ng-init directive
The ng-init directive initializes an AngularJS Application data. It is used to assign
values to the variables. In the following example, we initialize an array of
countries. We use JSON syntax to define the array of countries.
{locale:'en-GB',name:'United Kingdom'},
{locale:'en-FR',name:'France'}]">
...
15
Angular JS Tutorial
</div>
ng-model directive
The ng-model directive defines the model/variable to be used in AngularJS
Application. In the following example, we define a model named name.
<div ng-app="">
...
</div>
ng-repeat directive
ng-repeat directive repeats HTML elements for each item in a collection. In the
following example, we iterate over the array of countries.
<div ng-app="">
...
<ol>
</li>
</ol>
</div>
Example
The following example shows the use of all the above mentioned directives.
testAngularJS.htm
<html>
<title>AngularJS Directives</title>
16
Angular JS Tutorial
<body>
<h1>Sample Application</h1>
{locale:'en-GB',name:'United Kingdom'},
{locale:'en-FR',name:'France'}]">
<ol>
</li>
</ol>
</div>
<script
src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js">
</script>
</body>
</html>
Output
Open the file testAngularJS.htm in a web browser. Enter your name and see the
result.
17
Angular JS Tutorial
18
Angular JS Tutorial
19