M5 Angular JS
M5 Angular JS
‹#›
Ref: 01. 21CSL581-AngularJS.pdf ‹#›
● Assessment Details
● CIE Marks: 50
● 30marks - 10 marks for each experiments(observation, record, execution, viva)
● 20marks - 2 Tests (each 100marks: 60+40viva)
● SEE Marks: 50 (Practical Exam)
● Passing Criteria: 35% in SEE, 40% combined in CIE and SEE
‹#›
What is Javascript?
‹#›
‹#›
Databas
Front end Back end
e
Node: server
Mongo
M mongo It helps in JS file to run on
DB–NO
server
E express SQL
A angular Databas
Express: Buliding application e
N node
purely using node is difficult
so express framework is used
‹#›
●
‹#›
●
‹#›
Libraries/ API- it can be called as tools that contains classes, interface, methods.
Provides reusable functions to our code. Intend to provide reusable software functionality.
● React
● Selenium
‹#›
What is AngularJS?
‹#›
Ex: Pinterest, Gmap, Gmail, Facebook, Airbnb, paypal EX: E commerce website- amazon
A single page application (SPA) is a website that loads only one HTML
page and dynamically updates its content using JavaScript in
response to user actions, while a normal website loads a new,
complete page for each navigation link clicked, resulting in a full page
refresh with every transition; essentially, an SPA feels more like a
native app with seamless interaction, whereas a normal website
experiences noticeable page reloads between sections.
‹#›
AngularJS vs Angular ?
‹#›
‹#›
‹#›
Angular History
● Initial version of Angular(1.0) is called AngularJS which is developed using java script.
● Angular 1.x versions are called AngularJS
● From 2.0 onwards it is called Angular framework.
● Angular 2.x and later versions are called Angular framework
● Angular 2.x is completely re-written using Type script.
● Type script developed by Microsoft company
● Current version of Angular is 14
‹#›
Why do we Choose AngularJS?
‹#›
●A "single page application (SPA) refers to a web
application that loads only one HTML page and
dynamically updates its content using JavaScript, while
MVC (Model-View-Controller) is an architectural pattern
that separates application logic into distinct
components: the data model, the user interface (view),
and the logic that handles interactions between them,
which can be used to build both SPAs and traditional
multi-page applications; essentially, an SPA is a design
approach focused on user experience while MVC is a
way to structure application code regardless of the user
interface style.
‹#›
Core Features
‹#›
Pre-requisites
Ref: Basic.html
‹#›
‹#›
Java Script
‹#›
DOWNLOADING
INSTALLING ANGULAR JS
‹#›
● https://angularjs.org/
● DOWNLOAD ANGULARJS
● Download
● Method 1: Copy & paste in your
angular.js file in VS Code
● Method 2 (internet required): in script
tag – src attribute - Copy & paste the
link https://ajax.googleapis.com/ajax/
libs/angularjs/1.8.2/angular.min.js
‹#›
AngularJS Applications
‹#›
EXPRESSIONS IN ANGULAR
JS
‹#›
EXPRESSIONS IN ANGULAR JS
Ref: 1.1index.html
‹#›
<div ng-app="" ng-init="myCol='lightgreen'">
<input style="background-color:{{myCol}}" ng-model="myCol">
</div>
AngularJS Numbers-
<div ng-app="" ng-init="quantity=1;cost=5">
<p>Total in dollar: {{ quantity * cost }}</p>
</div>
‹#›
AngularJS Strings
‹#›
AngularJS Objects
‹#›
AngularJS Arrays
‹#›
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.
‹#›
MODULES IN ANGULAR JS
‹#›
● An AngularJS module defines an application.
● The module is a container for the different parts of an application & for the application
controllers.
● Controllers always belong to a module.
● The [] parameter in the module definition can be used to define dependent modules.
● Without the [] parameter, you are not creating a new module, but retrieving an existing one.
● A module is created by using the AngularJS function angular.module
<div ng-app="myApp">...</div> an HTML element in which the application will run
<script>var app = angular.module("myApp", []);</script>
can add controllers, directives, filters, to AngularJS application
‹#›
Adding a Controller
<!DOCTYPE html><html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl"> AngularJS has a set of
{{ firstName + " " + lastName }} built-in directives
</div> which you can use to
<script> add functionality to
var app = angular.module("myApp", []); your application.
app.controller("myCtrl", function($scope) { Ref: A-controller
$scope.firstName = "John";
$scope.lastName = "Doe";
});
</script></body></html>
‹#›
DIRECTIVES IN ANGULAR JS
‹#›
DIRECTIVES IN ANGULAR JS
‹#›
● The ng-app directive initializes an AngularJS application.
● The ng-init directive initializes application data.
● The ng-model directive binds the value of HTML controls (input, select, textarea) to application data.
<!DOCTYPE html><html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-app="" ng-init="firstName='John'">
<p>Input something in the input box:</p>
<p>Name: <input type="text" ng-model="firstName"></p>
<p>You wrote: {{ firstName }}</p>
</div></body></html>
Ref: A-Directives
‹#›
The ng-model directive binds the value of HTML controls (input, select, textarea) to application data.
‹#›
Data Binding
●The {{ firstName }} expression, in the example above, is an AngularJS data binding expression.
●Data binding in AngularJS binds AngularJS expressions with AngularJS data.
●{{ firstName }} is bound with ng-model="firstName".
‹#›
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<h2>Cost Calculator</h2>
‹#›
NG-APP DIRECTIVE
IN ANGULAR JS
‹#›
NG-APP DIRECTIVE
● Initialization Point: ng-app is an AngularJS directive that specifies the root element where
AngularJS will initialize.
● It tells AngularJS where your application begins.
● In AngularJS, we create single-page applications (SPAs) where most of the application
logic resides on a single HTML page.
● The ng-app directive defines the boundary of your SPA.
● The ng-app directive will auto-bootstrap (automatically initialize) the application when a
web page is loaded.
Ref: ng-app.html
1.1index.html ‹#›
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.
Ref: ng-app.html
1.1index.html ‹#›
NG-REPEAT IN
ANGULAR JS
‹#›
NG-REPEAT IN ANGULAR JS
Ref: A-repeat.html
1.1index.html ‹#›
Create New Directives
In addition to all the built-in AngularJS directives, you can create your own directives.
New directives are created by using the .directive function.
To invoke the new directive, make an HTML element with the same tag name as the new directive.
When naming a directive, you must use a camel case name, w3TestDirective, but when invoking it, you must use - separated
name, w3-test-directive:
You can invoke a directive by using:
Element name
Attribute
Class
Comment Element name <w3-test-directive></w3-test-directive>
Ref: newdirective.html Attribute <div w3-test-directive></div>
Class <div class="w3-test-directive"></div>
Comment <!-- directive: w3-test-directive -->
‹#›
NG-MODEL & NG-BIND
DIRECTIVE
IN ANGULAR JS
‹#›
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.
Ref: 1.2fuct_forms.html
‹#›
MVC
ANGULAR JS
‹#›
MVC IN ANGULAR JS
‹#›
REAL LIFE EXAMPLE OF MVC
‹#›
MVC Concepts
‹#›
<html ng-app="myApp">
<script src="angularjs.js"></script>
<script>
var app = angular.module("myApp", []);
app.controller("myController", function($scope) {
$scope.message = "Welcome";
});
</script>
<body ng-controller="myController">
<div >
<h2>{{message}}</h2>
<h2 ng-bind="message"></h2>
</div >
</body>
</html>
‹#›
AngularJS Module:
angular.module("myApp", [])
This line defines an AngularJS module named "myApp" using the
angular.module function.
The first parameter is the name of the module, and the second parameter is
an array of dependencies (currently empty in this case).
Modules in AngularJS are used to encapsulate different parts of an
application, such as controllers, services, and directives.
Ref: 2.1mvc.html
‹#›
Controller Definition:
.controller("myController", function($scope) { ... })
This part defines an AngularJS controller named "myController" within the
"myApp" module.
Controllers are responsible for handling the logic and data of a specific
view.
The controller function takes the $scope parameter, which is an object that
serves as the model for the view.
Ref: 2.1mvc.html
‹#›
Controller Function:
function($scope) { ... }
This is the controller function that gets executed when the "myController"
controller is instantiated.
It takes the $scope object as a parameter, allowing you to interact with the
view.
‹#›
SCOPE IN ANGULAR JS
Ref: 3.1scope.html
‹#›
SCOPE IN ANGULAR JS
● The scope is the binding part between the HTML (view) and the JavaScript (controller).
● The scope is an object with the available properties and methods.
● The scope is available for both the view and the controller.
Ref: 3.1scope.html
‹#›
Understanding the Scope
● The $scope is glue between a controller and view (HTML). It transfers data from the
controller to view and vice-versa.
VIEW MODEL
Ref: 3.1scope.html
‹#›
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
Ref: 3.1scopeTask1.html
‹#›
$scope in AngularJS is a built-in object which basically binds the “controller” and the “view”. One can define
member variables in the scope within the controller which can then be accessed by the view.
Code Explanation:
● The name of the module is “app”
● The name of the controller is “HelloWorldCntrl”
● Scope object is the main object which is used to pass information from the controller to the view.
● Member variable added to scope object
‹#›
key uses of $scope
● Data Binding:
● $scope facilitates two-way data binding between the controller and the view.
● Any changes made to the properties of $scope in the controller are automatically
reflected in the view, and vice versa.
‹#›
key uses of $scope
● Dependency Injection: A software design pattern that deals with how components get
hold of their dependencies.
● It facilitates you to divide your application into multiple different types of components
which can be injected into each other as dependencies.
● $scope is injected into the controller function by AngularJS's dependency injection
system.
● This means you don't have to explicitly create the $scope object;
● AngularJS provides it .
‹#›
key uses of $scope
‹#›
key uses of $scope
● Event Handling:
● $scope allows you to handle events in the controller and trigger actions in the view.
● For example, you can use $scope to define functions in the controller that respond to user
interactions, such as button clicks.
‹#›
‹#›
Understanding the Scope
● Isolated scope
● Shared scope
● Inherited scope
‹#›
● Shared scope: directive and controllers share the scope and the data. We cannot pass data
explicitly to the directive.
● Inherited scope: directive inherits the scope of the controller. We cannot pass data explicitly to the
directive.
● Isolated scope: directive and controllers don’t share the data and scope. We can pass data either
as a string or an object explicitly to the directive.
‹#›
‹#›
DATA BINDING IN
ANGULAR JS
‹#›
What is data binding?
● Data binding is a process of combining data between the model and the view.
‹#›
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
Ref: ng-bind_ng-change.html
‹#›
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
‹#›
What are Events?
● Within AngularJS we can register event listeners that will listen for said events and perform some
action whenever they occur.
● Events are "things" that happen to HTML elements.
● AngularJs can "react" on these events.
● An event can be a vast number of things, Here are some examples of
events:
● hovering over a hyperlink
● double-clicking on an object on the page
● An HTML web page has finished loading
● An HTML input field was changed
● An HTML button was clicked
thankfully Angular has directives that cover a lot of these.
‹#›
Mouse Events
● 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.
Ref: ng-bind_ng-change.html
‹#›
KeyBoard Events
‹#›
Copy Paste Events
Ref: copy_paste_event.html
‹#›
NG-SHOW AND NG-HIDE
DIRECTIVES IN
ANGULAR JS
‹#›
NG-SHOW DIRECTIVE
● The ng-show directive shows the specified HTML element if the expression evaluates to true,
otherwise the HTML element is hidden.
Ref: ng-show_ng-hide.html
‹#›
NG-HIDE DIRECTIVE
● The ng-hide directive hides the HTML element if the expression evaluates to true.
● ng-hide is also a predefined CSS class in AngularJS, and sets the element's display to none.
Ref: ng-show_ng-hide.html
‹#›
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 by advising the user about the current state.
Ref: 1.2fuct_forms.html
3.2taskcheck.html ‹#›
Form states
● Form is a collection of related individual controls grouped together. For example, Login
form -> allows users to input their credential to enter the application.
● Angular Form has some additional capabilities than plain HTML forms.
● It gives developers more control on how to communicate with the form.
● While creating Angular form, AngularJS creates an instance of FormController.
Ref: 1.2fuct_forms.html
3.2taskcheck.html ‹#›
Properties
‹#›
FILTERS IN
ANGULAR JS
‹#›
Filters
Ref: 3.2filters.html
‹#›
AngularJS Filters
Ref: 3.2filters.html
‹#›
FORMATS FOR DATE FILTER
●
The date filter formats a date to a specified format.
● "yyyy" year (2023)
● "yy" year (23)
● "y" year (2023)
● "MMMM" month (January)
● "MMM" month (Jan)
● "MM" month (01)
● "M" month (1)
Ref: 3.2filters.html
‹#›
FORMATS FOR DATE FILTER
Ref: 3.2filters.html
‹#›
FORMATS FOR TIME IN DATE FILTER
Ref: 3.2filters.html
‹#›
FORMATS FOR TIME IN DATE FILTER
Ref: 3.2filters.html
‹#›
FORMATS FOR DATE FILTER
● The format value can also be one of the following predefined formats:
● "short" same as "M/d/yy h:mm a" (1/5/23 9:05 AM)
● "medium" same as "MMM d, y h:mm:ss a" (Jan 5, 2023 9:05:05 AM)
● "shortDate" same as "M/d/yy" (1/5/23)
● "mediumDate" same as "MMM d, y" (Jan 5, 2023)
● "longDate" same as "MMMM d, y" (January 5, 2023)
● "fullDate" same as "EEEE, MMMM d, y" (Tuesday, January 5, 2023)
● "shortTime" same as "h:mm a" (9:05 AM)
● "mediumTime" same as "h:mm:ss a" (9:05:05 AM)
‹#›
orderBy Filter
‹#›
search Filter
<ul>
<li ng-repeat="item in items | filter: searchText">
{{item }}</li>
</ul>
‹#›
Limit-to filter IN
ANGULAR JS
‹#›
limitTo
<ul>
<li ng-repeat="item in items | orderBy: ‘name' | limitTo: 3">
{{ item }}</li>
</ul>
‹#›
Thank You
‹#›