WebProgramming-Section4
WebProgramming-Section4
[email protected] 31.03.2024 4
WHY SHOULD YOU USE ANGULARJS
§ Two-Way Data Binding: Unlike other template systems, AngularJS
templating system follows two-way data binding. In this process, first, the
template is compiled on the browser and produces a live view. Here, the
changes in the view appear in the model, and any changes in the model
immediately propagated to the view.
[email protected] 31.03.2024 5
§ Upon completion of this unit, you should be able to:
[email protected] 31.03.2024 6
§ Data Binding: Synchronization of information between model and view.
[email protected] 31.03.2024 7
§ We can use the following two styles to use jQuery library to write and execute
code in HTML document.
1. Local Installation
[email protected] 31.03.2024 8
§ For local installation, we can download the AngularJS library from its official website
https://angularjs.org/. When you click “download” button on the home page, the following
window will be appeared as shown in figure 8.1.
You can select the required version from the popup window
and download it on your computer.
After downloading, copy the AngularJS file “angular.min.js”
in the directory of your project and then reference it.
[email protected] 31.03.2024 9
§ If you are using CDN version of AngularJS, you need to provide the link of that AngularJS file
instead of downloading it. You can simply include the AngularJS library in to your HTML
document as shown in code 8.1.
[email protected] 31.03.2024 10
[email protected] 31.03.2024 11
§ An expression is a combination of one or more constants, variables,
operators and functions that represent a value.
§ In AngularJS, an expression is surrounded with double braces
“{{expression}}”.
§ They are used to bind application data to HTML. Like JavaScript,
the AngularJS expression can contain operators and variables.
[email protected] 31.03.2024 12
[email protected] 31.03.2024 13
AngularJS expressions are like JavaScript applications. However, they can not contain
conditional statements and loops.
[email protected] 31.03.2024 14
§ Create a simple AngularJS expression and display “Hello World” in your
output.
[email protected] 31.03.2024 15
§ AngularJS extends HTML functionality by providing new attributes
called directives. These directives are special functions that attach
specific behavior with the DOM elements.
§ These special attributes start with “ng-” prefix where “ng-” stand for
angular. AngularJS has a library of built in directives which are used to
extend HTML functionality.
§ For example, the static HTML code does not have the feature to update
the date in a web page continuously.
§ To enable an automatic updating of date in HTML, AngularJS can
provide a directive to display an advance form of date picker widget
(application code).
§ These directives start with the prefix “ng-”.
[email protected] 31.03.2024 16
§ ng-app − This directive starts an AngularJS Application.
• The ng-model directive creates a model variable named name, which can
be used with the HTML page and within the div having ng-app directive.
• The ng-bind then uses the name model to be displayed in the HTML
<span> tag whenever user enters input in the text box.
[email protected] 31.03.2024 18
§ “ng-app” directive starts an AngularJS application.
§ It automatically
initializes or bootstraps the application by defining the root
element when web page containing AngularJS application is loaded.
§ It is also used to load various AngularJS modules.
§ The following example shows the syntax of “ng- app” attribute of a div
element.
<div ng-app="">
:
</div>
[email protected] 31.03.2024 19
§ “ng-init” initializes an AngularJS application data.
§ It is used to define a local variable which can be used in the
application when required.
§ The following example, initializes name of the person by using
“ng-init”.
<div ng-init="firstName='Ali’”>
:
</div>
[email protected] 31.03.2024 20
§ “ng-model” binds an element with the property to be used in
AngularJS application.
§ The following example shows the binding of input type “text”,
using “ng-model” name.
[email protected] 31.03.2024 21
§ “ng-repeat” directive repeats html elements for each item in a
collection.
§ In following example, we have iterated over array of countries.
<div ng-repeat="">
Name: <input type="text" ng-model="name">
</div>
§ The code 8.3 demonstrates the use “ng-init” and “ng-repeat” example.
[email protected] 31.03.2024 22
[email protected] 31.03.2024 23
Output of code 8.3 is shown in figure 8.4. In this code “ng-init” initializes names and “ng-
repeat” is repeating HTML code to display each item of the array.
[email protected] 31.03.2024 24
§ Model View Controller or MVC as it is popularly called, is a software design pattern for
• 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.
[email protected] 31.03.2024 25
§ MVC is popular because it isolates the application logic from the user interface layer and
§ The controller receives all requests for the application and then works with the model to
§ The view then uses the data prepared by the controller to generate a final presentable response.
[email protected] 31.03.2024 26
§ 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.
[email protected] 31.03.2024 27
§ Data Binding is a process through which we can establish a link between the
data and the consumer frontend in a way that it gets synchronized
automatically.
§ AngularJS provides this feature in an effective way. This kind of binding links
the data with the frontend at all times. Once the data changes, the display gets
updated at the same time and vice versa. Thus, this update is being processed
on all change events of the data.
§ Prior to AngularJS, the data binding was expedited in one direction only. The
model and the template (data source) were merged to form a view (consumer
frontend) only once. So, the view was generated after taking a snapshot of the
data at any one time.
§ The change in the data was not constantly being tracked for the view to get
update as shown in figure 8.5.
[email protected] 31.03.2024 28
[email protected] 31.03.2024 29
§ Twoway data binding maintains and continues update relationship between the
model and the view by using AngularJS.
§ This
is achieved by placing the view at a central position on the theme as
shown in figure 8.6.
[email protected] 31.03.2024 30
§ 8.5.1. One way building
§ “ng-model” directive can be used in its basic form for one way data binding.
§ Its
application is shown in code 8.4. Code 8.4 describes the application of “ng-
model” in form.
[email protected] 31.03.2024 31
[email protected] 31.03.2024 32
§ Output of code 8.4 is shown in figure 8.7.
§ The phrase “AIOU” is displayed in the text field as it is binded
from “scope.name”.
[email protected] 31.03.2024 33
§ “ng-model” directive can also be used for two way data binding. Its application is shown in code 8.5.
[email protected] 31.03.2024 34
The output of code 8.5 is shown in figure 8.8 and 8.9.
Two way binding is achieved in this code as the phrase entered in the text field will appear
instantaneously in the display.
We are going to change the text "AIOU" with "Hello. I am new to angular!”.
[email protected] AngularJS controller controls the flow of data through its properties. 31.03.2024 36
31.03.2024 37
When we click on “Hello”, this text will be replaced
by “AIOU” as shown in figure 8.11.
[email protected] 31.03.2024 38
§ Write a small code to attach a property of “text-color” inside a
controller and display the effect of this property in the output.
[email protected] 31.03.2024 39
§ A scope is an object which is shared by the controller and the view.
§ This object joins the functionality of the controller with the views.
As the controller defines the properties on the scope, the views uses
these properties.
§ AngularJS keeps the controller and the view is synchronized by
implementing two way data binding.
§ Code 8.7 demonstrates the use of scope in AngularJS.
[email protected] 31.03.2024 40
[email protected] 31.03.2024 41
In the above example “scope. uniname” is being assigned a value “AIOU” and is being transferred to
“{{uniname}}” before it is displayed in the heading. Output of code 8.7 is shown in figure 8.12.
[email protected] 31.03.2024 42
§ AngularJS Filters is an AngularJS code that reads a certain amount
of data and transforms into an output pattern by adding or removing
certain elements.
§ It can change or update the data to display it on user screen without
changing its original format.
§ Table 8.1 list down some important filters.
[email protected] 31.03.2024 43
Filters can be added to expressions by using the pipe character |, followed by a
filter. Some of the filters are discussed below.
[email protected] 31.03.2024 44
The uppercase filter format strings to upper case. Code 8.8
demonstrates the use of uppercase filter.
31.03.2024 45
Output of code 8.8 is shown in figure 8.13. The phrase “world” is being displayed after converting it into upper case.
[email protected] 31.03.2024 46
§ The lowercase filter string to lower case.
§ Here we have addes lowercase filter to print first name in all
lowercase letters asshown in example.
[email protected] 31.03.2024 47
§ To display only required subjects, we use subjectname as filter.
§ This can be explained with the help of the following example.
<ul>
<li ng-repeat ="subject in student.subjects | orderBy: 'marks'" >
<li>
{{ subject.name + ', marks:' + subject.marks }}
</li>
</ul>
[email protected] 31.03.2024 49
[email protected] 31.03.2024 50
Please note that the figure of 100 is automatically formatted with the currency form.
[email protected] 31.03.2024 51