Introduction to Angular Js (1)
Introduction to Angular Js (1)
TO ANGULAR JS
What is AngularJS?
■ AngularJS is a JavaScript framework that is used to build
browser based Web applications.
■ It is an open source project, which means it can be freely
used, changed and shared by anyone.
■ It is useful in building single-page Web applications to
load varying content dynamically based on user actions.
■ 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.
What is AngularJS?
■ It is written in JavaScript with a reduced jQuery
library called jQuery lite.
■ It is very useful for Single Page Application
(SPA).
■ It was originally developed in 2009 / 2010 by
Misko Hevery and Adam Abrons. It is now
maintained by Google.
■ Latest version of AngularJs is 1.8.2
What is AngularJS?
■ The objective of AngularJS is to provide a
framework that makes it easy to implement well-
designed and well-structured Web pages and
applications
■ AngularJS makes Web application and the code
very simple to write, test, and maintain.
Why do we Choose
AngularJS?
■ Some of the reasons:
■ It makes us implement MVC and makes it easy to
correctly implement MVC.
■ It provides simple and flexible filters that help us to
easily format data
■ AngularJS applications use less code than traditional
JavaScript applications
■ AngularJS applications need less Document Object
Model (DOM) manipulation.
Core Features
■ The core features of AngularJS are as follows −
■ MVC
■ Data-binding
■ Scope
■ Controller
■ Services
■ Filters
■ Directives
■ Templates
■ Routing
■ Dependency Injection etc
Pre-requisites
■ Html
■ Css
■ Javascript
■ Jquery
DOWNLOADING
INSTALLING
ANGULAR JS
DIRECTIVES &
EXPRESSIONS IN
ANGULAR JS
DIRECTIVES IN ANGULAR
JS
■ AngularJS lets you extend HTML with new
attributes called Directives.
■ AngularJS has a set of built-in directives which
offers functionality to your applications.
■ AngularJS also lets you define your own directives.
DIRECTIVES IN ANGULAR
JS
■ AngularJS directives are extended HTML attributes
with the prefix ng-.
■ The ng-app directive initializes or starts an
AngularJS application.
EXPRESSIONS IN
ANGULAR
■ AngularJS expression isJS
similar to JavaScript code snippets.
■ It works with numbers and strings.
– {{5+6}}
– {{“Hello Student!”}}
■ It can also work with JavaScript objects and arrays.
– {{ user.name }}
– {{ items[index] }}
■ You can write expressions wherever you like, AngularJS will
simply resolve the expression and return the result.
AngularJS Expressions
vs. JavaScript
Expressions
■ Like JavaScript expressions, AngularJS expressions can
contain literals, operators, and variables.
■ Unlike JavaScript expressions, AngularJS expressions
can be written inside HTML.
■ AngularJS expressions do not support conditionals,
loops, and exceptions, while JavaScript expressions do.
■ AngularJS expressions support filters, while JavaScript
expressions do not.
NG-MODEL & NG-
BIND DIRECTIVE
IN ANGULAR JS
PRESENTER: MOHAMMAD ADIL
NG-MODEL DIRECTIVE
■ With the ng-model directive you can bind the
value of an input field to a variable created in
AngularJS.
■ The ng-model directive binds the value of HTML
controls (input, select, textarea) to application
data.
■ The ng-bind directive tells AngularJS to replace
the content of an HTML element with the value of
a given variable, or expression.
NG-INIT
DIRECTIVE
IN ANGULAR JS
NG-INIT DIRECTIVE
■ The ng-init directive initializes application data
like variables, arrays, objects, array of
objects etc.
■ It defines the initial value for an AngularJS
application and assigns values to the variables.
MVC IN
ANGULAR JS
PRESENTER: MOHAMMAD ADIL
MVC IN ANGULAR JS
app.controller("myController",function($scope)
{
$scope.name = "";
});
■ Controllers are JavaScript objects which have properties and
functions.
■ We use a controller in an application by using “ng-controller”
directive.
CONTROLLERS
■ Responsibilities of Controllers
■ Making available Data to UI
■ Managing presentation
■ Handling user inputs
■ Processing data
DATA BINDING IN
ANGULAR JS
OBJECTIVES
■ WHAT IS DATA BINDING
■ TYPES OF DATA-BINDING
What is data binding?
■ Data binding is a process of combining data
between the model and the view.
TYPES OF DATA
BINDING
■ 1. ONE WAY DATA-BINDING
■ 2. TWO WAY DATA-BINDING
1. ONE WAY DATA-
BINDING
MODEL
VIEW
ONE WAY DATA-BINDING
■ One-way data binding in AngularJS means
binding data from Model to View.
■ In one way data- binding data flows from the
scope/controller to the view
■ 'ng-bind' is an angular directive used for
achieving one-way data binding
TWO WAY DATA-BINDING
MODEL
VIEW
TWO WAY DATA-BINDING
■ Two Way Data binding is the synchronization
between the model and the view.
■ When data in the model changes, the view
reflects the change, and when data in the view
changes, the model is updated as well.
■ 'ng-model' is an angular directive used for
achieving two-way data binding
EVENTS AND EVENT
HANDLING IN
ANGULAR JS
What are Events?
■ Events are "things" that happen to HTML
elements.
■ AngularJs can "react" on these events.
■ Here are some examples of events:
– An HTML web page has finished loading
– An HTML input field was changed
– An HTML button was clicked
Mouse Events
■ On Mouse click
■ ng-click
■ ng-mousedown
■ ng-mouseup
■ ng-dblclick
Mouse Events
■ Mouse events occur when the cursor moves
over an element, in this order:
■ ng-mouseover
■ ng-mouseenter
■ ng-mousemove
■ ng-mouseleave
ng-change directive
■ The ng-change directive tells AngularJS what to do when
the value of an HTML element changes like textbox, select,
textarea.
■ The ng-change directive requires a ng-model directive to
be present.
■ The ng-change event is triggered at every change in the
value. It will not wait until all changes are made, or when
the input field loses focus.
■ The ng-change event is only triggered if there is a actual
change in the input value, and not if the change was made
from a JavaScript.
ng-focus and ng-blur
directive
■ The ng-focus directive tells AngularJS what to do
when an HTML element gets focus.
■ ng-keydown
■ ng-keypress
■ ng-keyup
Copy Paste Events
■ Executes when do some cut copy paste
operation on input fields.
■ ng-copy
■ ng-cut
■ ng-paste
NG-REPEAT IN
ANGULAR JS
NG-REPEAT IN ANGULAR
JS
■ The ng-repeat directive repeats a set of HTML, a
given number of times.
■ The set of HTML will be repeated once per item in
a collection.
■ The collection must be an array or an object.
NG-REPEAT IN ANGULAR
JS
expression An expression that specifies
how to loop the collection.
x in records
■ 2. <ng-view></ng-view>
VIEW MODEL
Understanding the
Scope
■ As we have seen in the controller section, we can
attach properties and methods to the $scope
object inside controller function.
■ The view can display $scope data using an
expression, ng-model, or ng-bind directive
Understanding the
Scope
■ The three types of scopes are:
– Isolated scope
– Shared scope
– Inherited scope
Root Scope
■All applications have a $rootScope which is
the scope created on the HTML element that
contains the ng-app directive.
■The rootScope is available in the entire
application.
■If a variable has the same name in both the
current scope and in the rootScope, the
application uses the one in the current scope.
Scope Inheritance
PARENT
CHILD
FORM
VALIDATION
IN ANGULAR-JS
Introduction to Forms
Validation
■ Forms are the major way users communicate with the
apps we develop and are an important mechanism of
modern Websites and applications.
■ They are used to collect data form the users.
■ The data collected by the form is validated before
sending it to the server.
■ AngularJS continuously oversees the status of the
form and their input fields such as input, textarea,
select and help us advise the user about the current
state.