SlideShare a Scribd company logo
www.webstackacademy.com
Data and Event Handling
Angular
www.webstackacademy.comwww.webstackacademy.com
Interpolation
(Connecting data with view)
www.webstackacademy.com
Interpolation
• At high level component can be broken into two parts:
 View - Which takes care of the look & Feel (HTML + CSS)
 Business logic - Which takes care of the user and server interactivity (TypeScript -> JavaScript)
{ } <#>
courses.component.ts
courses.services.ts
courses.component.html
courses.component.css
Data
How to access data from into template?
www.webstackacademy.com
Interpolation
• The major difference between writing them in a 'vanilla' fashion (how we have done during
JavaScript module) and Angular is we have followed certain rules
• The main goal of these rules is to de-couple the View (HTML + CSS) and Business Logic
• This makes components re-usable, modular and maintainable which is required for SPA
• In real-world applications these two need to work together by communicating properties
(variables, objects, arrays, etc..) from the component class to the template, you can use
interpolation
• This is achieved with the notation {{ propertyName }} in the template. With this
approach whenever we are changing the functionality, the template don’t need to change.
• This way of connection is called as Binding
www.webstackacademy.com
Interpolation
@Component({
selector: 'courses',
template: `
<h2> {{title}}</h2>
<ul> {{courses}}</ul>
`
})
export class CoursesComponent {
title = 'List of courses in WSA:';
courses = ["FullStack","FrontEnd","BackEnd"]
}
{}
<#>
www.webstackacademy.comwww.webstackacademy.com
Binding
(Connecting data with view)
www.webstackacademy.com
Binding – Big picture
www.webstackacademy.com
DOM - Revisiting
 The Document Object Model (DOM) is a cross-platform and language-independent
application programming interface
 The DOM, is the API through which JavaScript interacts with content within a website
 The DOM API is used to access, traverse and manipulate HTML and XML documents
 The HTML is represented in a tree format with each node representing a property
 Nodes have “parent-child” relationship tracing back to the “root” <html>
www.webstackacademy.com
DOM - Revisiting
www.webstackacademy.com
DOM and HTML
<html>
<body>
<h1> Courses in WSA </h1>
<ul>
<li> FullStack web developer</li>
<li> Frontend web developer </li>
<li> Backend web developer </li>
<ul>
</body>
</html>
www.webstackacademy.com
DOM and HTML – Zooming In!
www.webstackacademy.com
Attribute Binding (One way: Business logic -> View)
export class BindingComponent {
imageLink = "http://www.testimage.com/side-image.png";
}
<p><b>Example of property binding: Image</b></p>
<img [src]= "imageLink"/>
{}
<#>
www.webstackacademy.com
HTML - DOM element object
Following are some of the properties that can be used on all HTML elements
Property Description
clientHeight Returns the height of an element, including padding
Id Sets or returns the value of the id attribute of an element
innerHTML Sets or returns the content of an element
Style Sets or returns the value of the style attribute of an element
textContent Sets or returns the textual content of a node and its descendants
Title Sets or returns the value of the title attribute of an element
nextSibling Returns the next node at the same node tree level
nodeType Returns the node type of a node
www.webstackacademy.comwww.webstackacademy.com
Event Binding
(Capturing events from the view)
www.webstackacademy.com
Event Binding
• In property binding, typically the changes done in the business logic (functionality part) is
getting accessed from the view. It was achieved by the []notation, in combination with the
template (HTML) elements
• Event binding is the opposite of property binding where events from the template is sent back
to the business logic in form of event notification functions.
• Similar to how it was done with vanilla JavaScript, each event to have a call-back function along
with the event in order to notify in case of events
• User actions such as clicking a link, pushing a button, and entering text raise DOM events.
• This event binding is achieved with the ()notation
www.webstackacademy.com
Attribute Binding (One way: Business logic -> View)
{}
<#><button (click)="mySaveFunction()">Save</button>
export class EbindingComponent {
mySaveFunction() {
console.log ("Button was pressed”);
}
}
www.webstackacademy.com
More Event Handling…
<input (keyup.enter)="myKeyPress()">
Event Filtering: Event handlers can be called on a filtering basis.
View variables: Variables can be defined in the view in order to send value to business logic. These
variables are also called as Template Variables.
<input #userInput
(keyup.enter)="myKeyPressWithValue(userInput.value)">
www.webstackacademy.com
Event Binding
Methods Description
Click The event occurs when the user clicks on an element
Dbclick The event occurs when the user double-clicks on an element
Keyup The event occurs when the user releases a key
Mouseover The event occurs when the pointer is moved onto an element, or onto one of its children
Submit The event occurs when a form is submitted
Following are some of the important events which is used in event binding use-cases
www.webstackacademy.comwww.webstackacademy.com
Two way Binding
(Binding Business logic & View in both direction)
www.webstackacademy.com
Two way Binding
• One way binding in Angular is achieved in the following ways:
 From View to Business Logic (Event binding) : ()notation
 From Business logic to view (Data binding) : []notation
• They are also used in a combined manner. An event from the view
triggers a change in a HTML attribute (View->Business Logic->View)
[Ex: Changing another paragraph’s background with a button click
event]
• However handling two way binding is still not feasible with such
approaches.
• There are some ways we have achieved (ex: $event), but parameter
passing quite complex
• In order to achieve two way binding ngModel directive is used
www.webstackacademy.com
What is ngModel directive?
• ngModel is one of the directives supported by Angular to achieve two-way binding
• A directive is a custom HTML element (provided by Angular) that is used to extend the
power of HTML (More on this during next chapter)
• To set up two-way data binding with ngModel, you need to add an input in the template
and achieve binding them using combination of both square bracket and parenthesis
• With this binding whatever changes you are making in both view and business-logic get
synchronized automatically without any explicit parameter passing
<input [(ngModel)]="userInput" (keyup.enter)="userKeyPress()"/>
www.webstackacademy.com
How to use ngModel?
• ngModel is part of the Forms Module in Angular. It is not imported by default, hence it need to be done
explicitly by adding it into the app.module.ts file
www.webstackacademy.comwww.webstackacademy.com
Pipes
(Displaying data in a better way)
www.webstackacademy.com
Pipes
• Pipe takes input in the view and transforms into a better readable / understandable format
• Some of the standard pipes are supported, users can create their own pipes (custom pipes)
• Multiple pipes can be combined together to achieve the desired functionality
• Pipes are used along with text, from the template along with interpolation
• Pipes make application level handling of text much easier. Multiple pipes can also be combined
together
• In case of multiple pipes, output of one pipe is given as input of another
Pipe usage: {{ userText | Pipe }}
www.webstackacademy.com
Standard Pipes
Methods Description
Currency Formats the current input in form of currency (ex: currency:’INR’)
Date Transforms dates (shortDate | shortTime | fullDate)
Decimal Formats decimal numbers (number: ‘2.2-4’)
Lowercase Converts input into lower-case
Uppercase Converts input into upper case
Percent Convers into percentage format (percent:’2.2-5’)
www.webstackacademy.com
Exercise
• Using data binding, change the following HTML attributes by changing appropriate DOM
element:
• Font size
• Left Margin
• Top Margin
• Using event binding, handle the following events by having event handlers
• Mouse over
• Double click
• Change a HTML attribute of a text (ex: BackgroundColor) when the user click any of the
mouse events. Demonstrate view->business logic -> view binding
• Handle the following standard pipes
• Percentage
• Currency
www.webstackacademy.comwww.webstackacademy.com
Creating a Custom Pipe - GUI
(Implementing your own pipe functionality)
www.webstackacademy.com
Creating a pipe – GUI mode
 Step-1: Creating / Adding your pipe TS file
• Open your Angular project folder (ex: myFirstApp) in your editor (ex: VS code). Go to src/app folder.
• Add a new file in the following format: <pipe_name>.pipe.ts (ex: summarypipe.pipe.ts)
• Add the following lines into your new pipe file. Details given as comments.
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ // Pipe decorator to tell Angular that we are implementing a pipe
name: 'summarypipe' // Pipe name, which need to be used in interpolation
})
export class SummarypipePipe implements PipeTransform {
transform(value: any,…, args?: any[]): any {
// Implement your pipe functionality here
}
www.webstackacademy.com
Implements interface
 Implements interface ensures classes must follow a particular structure that is already defined
 Basically ensures the class is of same ‘shape’
interface Point {
xValue : number;
yValue : number;
}
class MyPoint implements Point {
xValue: number;
yValue: number;
}
class MyPoint implements Point {
xValue: number;
yValue: number;
zValue: number;
}
www.webstackacademy.com
PipeTransform Interface
 The PipeTransform interface is defined in Angular (ref – below)
 The transform method is where the custom pipe functionality is implemented, which guides the
compiler to have a look-up and execute in run-time
 It will accept any number of any type of arguments and return the transformed value which also can be
of any type
interface PipeTransform {
transform(value: any, ...args: any[]): any
}
Angular official documentation URL: https://angular.io/guide/pipes
www.webstackacademy.com
Creating a pipe – GUI mode
 Step-2: Make your pipe as a part of the module (app.module.ts file)
• Add your new pipe (ex: SummaryPipe) under the declarations sections of the module.
• Ensure you import appropriate pipe files, similar to components
@NgModule({
declarations: [
AppComponent,
SummaryPipe
],
imports: [
BrowserModule,
],
providers: [],
bootstrap: [AppComponent]
})
www.webstackacademy.com
Creating a pipe – GUI mode
 Step-3: Invoking / Using the pipe
 Pipe need to be invoked by the component from its template file
<h2>Custom pipe - Example</h2>
{{ myText | summarypipe }}
 Step-4: Save all and compile. U should be able to use your custom pipe now
www.webstackacademy.comwww.webstackacademy.com
Creating a Custom pipe - CLI
(CLI mode for custom pipes)
www.webstackacademy.com
Creating a component – CLI mode
• Sometimes you might find it little hard to create a component using GUI. In case you miss out
any steps, the app will not work.
• Angular CLI offers much more reliable way to create a pipe (similar to component and services).
Try out the following command, which will not only generate a component but also make
appropriate entries
$ ng g p myformat // Creates a new pipe
www.webstackacademy.com
Creating a pipe – CLI mode
www.webstackacademy.com
Exercise
• Create a custom pipe to find out the zodiac sign of the user
• Input is given as an entry in CSV file format as follows:
• Here are the Zodiac signs for all the months:
Rajan,9845123450,rajan@gmail.com,22/05/1990,Bangalore
• Aries (March 21-April 19)
• Taurus (April 20-May 20)
• Gemini (May 21-June 20)
• Cancer (June 21-July 22)
• Leo (July 23-August 22)
• Virgo (August 23-September 22)
• Libra (September 23-October 22)
• Scorpio (October 23-November 21)
• Sagittarius (November 22-December 21)
• Capricorn (December 22-January 19)
• Aquarius (January 20 to February 18)
• Pisces (February 19 to March 20)
www.webstackacademy.com
WebStack Academy
#83, Farah Towers,
1st Floor, MG Road,
Bangalore – 560001
M: +91-809 555 7332
E: training@webstackacademy.com
WSA in Social Media:
Ad

Recommended

Angular - Chapter 5 - Directives
Angular - Chapter 5 - Directives
WebStackAcademy
 
Angular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP Services
WebStackAcademy
 
Angular Data Binding
Angular Data Binding
Jennifer Estrada
 
Sharing Data Between Angular Components
Sharing Data Between Angular Components
Squash Apps Pvt Ltd
 
Angular 9
Angular 9
Raja Vishnu
 
Java Script ppt
Java Script ppt
Priya Goyal
 
Intro to React
Intro to React
Justin Reock
 
Angular Introduction By Surekha Gadkari
Angular Introduction By Surekha Gadkari
Surekha Gadkari
 
Angular data binding
Angular data binding
Sultan Ahmed
 
Angular - Chapter 3 - Components
Angular - Chapter 3 - Components
WebStackAcademy
 
Javascript 101
Javascript 101
Shlomi Komemi
 
JavaScript Tutorial
JavaScript Tutorial
Bui Kiet
 
Angular
Angular
Lilia Sfaxi
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to Javascript
Walid Ashraf
 
Html5 and-css3-overview
Html5 and-css3-overview
Jacob Nelson
 
Angular Directives
Angular Directives
iFour Technolab Pvt. Ltd.
 
Angular
Angular
Mouad EL Fakir
 
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Edureka!
 
Javascript and DOM
Javascript and DOM
Brian Moschel
 
JavaScript - An Introduction
JavaScript - An Introduction
Manvendra Singh
 
ReactJS presentation
ReactJS presentation
Thanh Tuong
 
Angular - Chapter 1 - Introduction
Angular - Chapter 1 - Introduction
WebStackAcademy
 
React js
React js
Rajesh Kolla
 
jQuery for beginners
jQuery for beginners
Arulmurugan Rajaraman
 
Javascript
Javascript
mussawir20
 
Angular modules in depth
Angular modules in depth
Christoffer Noring
 
Angular 8
Angular 8
Sunil OS
 
ASP.NET Web API
ASP.NET Web API
habib_786
 
Data binding in Angular fully Explained .pptx
Data binding in Angular fully Explained .pptx
ExtraID8
 
Angular 2 overview in 60 minutes
Angular 2 overview in 60 minutes
Loiane Groner
 

More Related Content

What's hot (20)

Angular data binding
Angular data binding
Sultan Ahmed
 
Angular - Chapter 3 - Components
Angular - Chapter 3 - Components
WebStackAcademy
 
Javascript 101
Javascript 101
Shlomi Komemi
 
JavaScript Tutorial
JavaScript Tutorial
Bui Kiet
 
Angular
Angular
Lilia Sfaxi
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to Javascript
Walid Ashraf
 
Html5 and-css3-overview
Html5 and-css3-overview
Jacob Nelson
 
Angular Directives
Angular Directives
iFour Technolab Pvt. Ltd.
 
Angular
Angular
Mouad EL Fakir
 
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Edureka!
 
Javascript and DOM
Javascript and DOM
Brian Moschel
 
JavaScript - An Introduction
JavaScript - An Introduction
Manvendra Singh
 
ReactJS presentation
ReactJS presentation
Thanh Tuong
 
Angular - Chapter 1 - Introduction
Angular - Chapter 1 - Introduction
WebStackAcademy
 
React js
React js
Rajesh Kolla
 
jQuery for beginners
jQuery for beginners
Arulmurugan Rajaraman
 
Javascript
Javascript
mussawir20
 
Angular modules in depth
Angular modules in depth
Christoffer Noring
 
Angular 8
Angular 8
Sunil OS
 
ASP.NET Web API
ASP.NET Web API
habib_786
 
Angular data binding
Angular data binding
Sultan Ahmed
 
Angular - Chapter 3 - Components
Angular - Chapter 3 - Components
WebStackAcademy
 
JavaScript Tutorial
JavaScript Tutorial
Bui Kiet
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to Javascript
Walid Ashraf
 
Html5 and-css3-overview
Html5 and-css3-overview
Jacob Nelson
 
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Edureka!
 
JavaScript - An Introduction
JavaScript - An Introduction
Manvendra Singh
 
ReactJS presentation
ReactJS presentation
Thanh Tuong
 
Angular - Chapter 1 - Introduction
Angular - Chapter 1 - Introduction
WebStackAcademy
 
Angular 8
Angular 8
Sunil OS
 
ASP.NET Web API
ASP.NET Web API
habib_786
 

Similar to Angular - Chapter 4 - Data and Event Handling (20)

Data binding in Angular fully Explained .pptx
Data binding in Angular fully Explained .pptx
ExtraID8
 
Angular 2 overview in 60 minutes
Angular 2 overview in 60 minutes
Loiane Groner
 
yrs of IT experience in enterprise programming
yrs of IT experience in enterprise programming
narasimhulum1623
 
Commit University - Exploring Angular 2
Commit University - Exploring Angular 2
Commit University
 
Angular2 + rxjs
Angular2 + rxjs
Christoffer Noring
 
What is your money doing?
What is your money doing?
Alfonso Fernández
 
Angular Project Report
Angular Project Report
Kodexhub
 
Angular Interview Questions & Answers
Angular Interview Questions & Answers
Ratnala Charan kumar
 
Presentation on angular 5
Presentation on angular 5
Ramesh Adhikari
 
Fly High With Angular - How to build an app using Angular
Fly High With Angular - How to build an app using Angular
Vacation Labs
 
Building an Angular 2 App
Building an Angular 2 App
Felix Gessert
 
Instant download Angular 2 Cookbook Frisbie pdf all chapter
Instant download Angular 2 Cookbook Frisbie pdf all chapter
ikraanfalan
 
Angular 2 - The Next Framework
Angular 2 - The Next Framework
Commit University
 
Angular 18 course for begineers and experienced
Angular 18 course for begineers and experienced
tejaswinimysoola
 
Angular2 with TypeScript
Angular2 with TypeScript
Rohit Bishnoi
 
Infosys Angular Interview Questions PDF By ScholarHat
Infosys Angular Interview Questions PDF By ScholarHat
Scholarhat
 
What is data binding in Angular
What is data binding in Angular
technicalChamber
 
Introduction to angular 2
Introduction to angular 2
Dhyego Fernando
 
Building Powerful Applications with AngularJS 2 and TypeScript - David Giard
Building Powerful Applications with AngularJS 2 and TypeScript - David Giard
ITCamp
 
Download full ebook of Angular 2 Cookbook Frisbie instant download pdf
Download full ebook of Angular 2 Cookbook Frisbie instant download pdf
kovencoitofm
 
Data binding in Angular fully Explained .pptx
Data binding in Angular fully Explained .pptx
ExtraID8
 
Angular 2 overview in 60 minutes
Angular 2 overview in 60 minutes
Loiane Groner
 
yrs of IT experience in enterprise programming
yrs of IT experience in enterprise programming
narasimhulum1623
 
Commit University - Exploring Angular 2
Commit University - Exploring Angular 2
Commit University
 
Angular Project Report
Angular Project Report
Kodexhub
 
Angular Interview Questions & Answers
Angular Interview Questions & Answers
Ratnala Charan kumar
 
Presentation on angular 5
Presentation on angular 5
Ramesh Adhikari
 
Fly High With Angular - How to build an app using Angular
Fly High With Angular - How to build an app using Angular
Vacation Labs
 
Building an Angular 2 App
Building an Angular 2 App
Felix Gessert
 
Instant download Angular 2 Cookbook Frisbie pdf all chapter
Instant download Angular 2 Cookbook Frisbie pdf all chapter
ikraanfalan
 
Angular 2 - The Next Framework
Angular 2 - The Next Framework
Commit University
 
Angular 18 course for begineers and experienced
Angular 18 course for begineers and experienced
tejaswinimysoola
 
Angular2 with TypeScript
Angular2 with TypeScript
Rohit Bishnoi
 
Infosys Angular Interview Questions PDF By ScholarHat
Infosys Angular Interview Questions PDF By ScholarHat
Scholarhat
 
What is data binding in Angular
What is data binding in Angular
technicalChamber
 
Introduction to angular 2
Introduction to angular 2
Dhyego Fernando
 
Building Powerful Applications with AngularJS 2 and TypeScript - David Giard
Building Powerful Applications with AngularJS 2 and TypeScript - David Giard
ITCamp
 
Download full ebook of Angular 2 Cookbook Frisbie instant download pdf
Download full ebook of Angular 2 Cookbook Frisbie instant download pdf
kovencoitofm
 
Ad

More from WebStackAcademy (20)

Webstack Academy - Course Demo Webinar and Placement Journey
Webstack Academy - Course Demo Webinar and Placement Journey
WebStackAcademy
 
WSA: Scaling Web Service to Handle Millions of Requests per Second
WSA: Scaling Web Service to Handle Millions of Requests per Second
WebStackAcademy
 
WSA: Course Demo Webinar - Full Stack Developer Course
WSA: Course Demo Webinar - Full Stack Developer Course
WebStackAcademy
 
Career Building in AI - Technologies, Trends and Opportunities
Career Building in AI - Technologies, Trends and Opportunities
WebStackAcademy
 
Webstack Academy - Internship Kick Off
Webstack Academy - Internship Kick Off
WebStackAcademy
 
Building Your Online Portfolio
Building Your Online Portfolio
WebStackAcademy
 
Front-End Developer's Career Roadmap
Front-End Developer's Career Roadmap
WebStackAcademy
 
Angular - Chapter 9 - Authentication and Authorization
Angular - Chapter 9 - Authentication and Authorization
WebStackAcademy
 
Angular - Chapter 6 - Firebase Integration
Angular - Chapter 6 - Firebase Integration
WebStackAcademy
 
Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming
WebStackAcademy
 
JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 10 - Strings and Arrays
WebStackAcademy
 
JavaScript - Chapter 15 - Debugging Techniques
JavaScript - Chapter 15 - Debugging Techniques
WebStackAcademy
 
JavaScript - Chapter 14 - Form Handling
JavaScript - Chapter 14 - Form Handling
WebStackAcademy
 
JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)
WebStackAcademy
 
JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
JavaScript - Chapter 11 - Events
JavaScript - Chapter 11 - Events
WebStackAcademy
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
WebStackAcademy
 
JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
JavaScript - Chapter 7 - Advanced Functions
JavaScript - Chapter 7 - Advanced Functions
WebStackAcademy
 
JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic Functions
WebStackAcademy
 
Webstack Academy - Course Demo Webinar and Placement Journey
Webstack Academy - Course Demo Webinar and Placement Journey
WebStackAcademy
 
WSA: Scaling Web Service to Handle Millions of Requests per Second
WSA: Scaling Web Service to Handle Millions of Requests per Second
WebStackAcademy
 
WSA: Course Demo Webinar - Full Stack Developer Course
WSA: Course Demo Webinar - Full Stack Developer Course
WebStackAcademy
 
Career Building in AI - Technologies, Trends and Opportunities
Career Building in AI - Technologies, Trends and Opportunities
WebStackAcademy
 
Webstack Academy - Internship Kick Off
Webstack Academy - Internship Kick Off
WebStackAcademy
 
Building Your Online Portfolio
Building Your Online Portfolio
WebStackAcademy
 
Front-End Developer's Career Roadmap
Front-End Developer's Career Roadmap
WebStackAcademy
 
Angular - Chapter 9 - Authentication and Authorization
Angular - Chapter 9 - Authentication and Authorization
WebStackAcademy
 
Angular - Chapter 6 - Firebase Integration
Angular - Chapter 6 - Firebase Integration
WebStackAcademy
 
Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming
WebStackAcademy
 
JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 10 - Strings and Arrays
WebStackAcademy
 
JavaScript - Chapter 15 - Debugging Techniques
JavaScript - Chapter 15 - Debugging Techniques
WebStackAcademy
 
JavaScript - Chapter 14 - Form Handling
JavaScript - Chapter 14 - Form Handling
WebStackAcademy
 
JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)
WebStackAcademy
 
JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
JavaScript - Chapter 11 - Events
JavaScript - Chapter 11 - Events
WebStackAcademy
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
WebStackAcademy
 
JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
JavaScript - Chapter 7 - Advanced Functions
JavaScript - Chapter 7 - Advanced Functions
WebStackAcademy
 
JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic Functions
WebStackAcademy
 
Ad

Recently uploaded (20)

FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
 
AI VIDEO MAGAZINE - June 2025 - r/aivideo
AI VIDEO MAGAZINE - June 2025 - r/aivideo
1pcity Studios, Inc
 
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
 
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
SOFTTECHHUB
 
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
 
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Alliance
 
Artificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdf
OnBoard
 
Security Tips for Enterprise Azure Solutions
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
 
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
Edge AI and Vision Alliance
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
The State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry Report
Liveplex
 
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Alliance
 
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
 
AI VIDEO MAGAZINE - June 2025 - r/aivideo
AI VIDEO MAGAZINE - June 2025 - r/aivideo
1pcity Studios, Inc
 
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
 
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
SOFTTECHHUB
 
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
 
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Alliance
 
Artificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdf
OnBoard
 
Security Tips for Enterprise Azure Solutions
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
 
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
Edge AI and Vision Alliance
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
The State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry Report
Liveplex
 
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Alliance
 

Angular - Chapter 4 - Data and Event Handling

  • 3. www.webstackacademy.com Interpolation • At high level component can be broken into two parts:  View - Which takes care of the look & Feel (HTML + CSS)  Business logic - Which takes care of the user and server interactivity (TypeScript -> JavaScript) { } <#> courses.component.ts courses.services.ts courses.component.html courses.component.css Data How to access data from into template?
  • 4. www.webstackacademy.com Interpolation • The major difference between writing them in a 'vanilla' fashion (how we have done during JavaScript module) and Angular is we have followed certain rules • The main goal of these rules is to de-couple the View (HTML + CSS) and Business Logic • This makes components re-usable, modular and maintainable which is required for SPA • In real-world applications these two need to work together by communicating properties (variables, objects, arrays, etc..) from the component class to the template, you can use interpolation • This is achieved with the notation {{ propertyName }} in the template. With this approach whenever we are changing the functionality, the template don’t need to change. • This way of connection is called as Binding
  • 5. www.webstackacademy.com Interpolation @Component({ selector: 'courses', template: ` <h2> {{title}}</h2> <ul> {{courses}}</ul> ` }) export class CoursesComponent { title = 'List of courses in WSA:'; courses = ["FullStack","FrontEnd","BackEnd"] } {} <#>
  • 8. www.webstackacademy.com DOM - Revisiting  The Document Object Model (DOM) is a cross-platform and language-independent application programming interface  The DOM, is the API through which JavaScript interacts with content within a website  The DOM API is used to access, traverse and manipulate HTML and XML documents  The HTML is represented in a tree format with each node representing a property  Nodes have “parent-child” relationship tracing back to the “root” <html>
  • 10. www.webstackacademy.com DOM and HTML <html> <body> <h1> Courses in WSA </h1> <ul> <li> FullStack web developer</li> <li> Frontend web developer </li> <li> Backend web developer </li> <ul> </body> </html>
  • 12. www.webstackacademy.com Attribute Binding (One way: Business logic -> View) export class BindingComponent { imageLink = "http://www.testimage.com/side-image.png"; } <p><b>Example of property binding: Image</b></p> <img [src]= "imageLink"/> {} <#>
  • 13. www.webstackacademy.com HTML - DOM element object Following are some of the properties that can be used on all HTML elements Property Description clientHeight Returns the height of an element, including padding Id Sets or returns the value of the id attribute of an element innerHTML Sets or returns the content of an element Style Sets or returns the value of the style attribute of an element textContent Sets or returns the textual content of a node and its descendants Title Sets or returns the value of the title attribute of an element nextSibling Returns the next node at the same node tree level nodeType Returns the node type of a node
  • 15. www.webstackacademy.com Event Binding • In property binding, typically the changes done in the business logic (functionality part) is getting accessed from the view. It was achieved by the []notation, in combination with the template (HTML) elements • Event binding is the opposite of property binding where events from the template is sent back to the business logic in form of event notification functions. • Similar to how it was done with vanilla JavaScript, each event to have a call-back function along with the event in order to notify in case of events • User actions such as clicking a link, pushing a button, and entering text raise DOM events. • This event binding is achieved with the ()notation
  • 16. www.webstackacademy.com Attribute Binding (One way: Business logic -> View) {} <#><button (click)="mySaveFunction()">Save</button> export class EbindingComponent { mySaveFunction() { console.log ("Button was pressed”); } }
  • 17. www.webstackacademy.com More Event Handling… <input (keyup.enter)="myKeyPress()"> Event Filtering: Event handlers can be called on a filtering basis. View variables: Variables can be defined in the view in order to send value to business logic. These variables are also called as Template Variables. <input #userInput (keyup.enter)="myKeyPressWithValue(userInput.value)">
  • 18. www.webstackacademy.com Event Binding Methods Description Click The event occurs when the user clicks on an element Dbclick The event occurs when the user double-clicks on an element Keyup The event occurs when the user releases a key Mouseover The event occurs when the pointer is moved onto an element, or onto one of its children Submit The event occurs when a form is submitted Following are some of the important events which is used in event binding use-cases
  • 20. www.webstackacademy.com Two way Binding • One way binding in Angular is achieved in the following ways:  From View to Business Logic (Event binding) : ()notation  From Business logic to view (Data binding) : []notation • They are also used in a combined manner. An event from the view triggers a change in a HTML attribute (View->Business Logic->View) [Ex: Changing another paragraph’s background with a button click event] • However handling two way binding is still not feasible with such approaches. • There are some ways we have achieved (ex: $event), but parameter passing quite complex • In order to achieve two way binding ngModel directive is used
  • 21. www.webstackacademy.com What is ngModel directive? • ngModel is one of the directives supported by Angular to achieve two-way binding • A directive is a custom HTML element (provided by Angular) that is used to extend the power of HTML (More on this during next chapter) • To set up two-way data binding with ngModel, you need to add an input in the template and achieve binding them using combination of both square bracket and parenthesis • With this binding whatever changes you are making in both view and business-logic get synchronized automatically without any explicit parameter passing <input [(ngModel)]="userInput" (keyup.enter)="userKeyPress()"/>
  • 22. www.webstackacademy.com How to use ngModel? • ngModel is part of the Forms Module in Angular. It is not imported by default, hence it need to be done explicitly by adding it into the app.module.ts file
  • 24. www.webstackacademy.com Pipes • Pipe takes input in the view and transforms into a better readable / understandable format • Some of the standard pipes are supported, users can create their own pipes (custom pipes) • Multiple pipes can be combined together to achieve the desired functionality • Pipes are used along with text, from the template along with interpolation • Pipes make application level handling of text much easier. Multiple pipes can also be combined together • In case of multiple pipes, output of one pipe is given as input of another Pipe usage: {{ userText | Pipe }}
  • 25. www.webstackacademy.com Standard Pipes Methods Description Currency Formats the current input in form of currency (ex: currency:’INR’) Date Transforms dates (shortDate | shortTime | fullDate) Decimal Formats decimal numbers (number: ‘2.2-4’) Lowercase Converts input into lower-case Uppercase Converts input into upper case Percent Convers into percentage format (percent:’2.2-5’)
  • 26. www.webstackacademy.com Exercise • Using data binding, change the following HTML attributes by changing appropriate DOM element: • Font size • Left Margin • Top Margin • Using event binding, handle the following events by having event handlers • Mouse over • Double click • Change a HTML attribute of a text (ex: BackgroundColor) when the user click any of the mouse events. Demonstrate view->business logic -> view binding • Handle the following standard pipes • Percentage • Currency
  • 27. www.webstackacademy.comwww.webstackacademy.com Creating a Custom Pipe - GUI (Implementing your own pipe functionality)
  • 28. www.webstackacademy.com Creating a pipe – GUI mode  Step-1: Creating / Adding your pipe TS file • Open your Angular project folder (ex: myFirstApp) in your editor (ex: VS code). Go to src/app folder. • Add a new file in the following format: <pipe_name>.pipe.ts (ex: summarypipe.pipe.ts) • Add the following lines into your new pipe file. Details given as comments. import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ // Pipe decorator to tell Angular that we are implementing a pipe name: 'summarypipe' // Pipe name, which need to be used in interpolation }) export class SummarypipePipe implements PipeTransform { transform(value: any,…, args?: any[]): any { // Implement your pipe functionality here }
  • 29. www.webstackacademy.com Implements interface  Implements interface ensures classes must follow a particular structure that is already defined  Basically ensures the class is of same ‘shape’ interface Point { xValue : number; yValue : number; } class MyPoint implements Point { xValue: number; yValue: number; } class MyPoint implements Point { xValue: number; yValue: number; zValue: number; }
  • 30. www.webstackacademy.com PipeTransform Interface  The PipeTransform interface is defined in Angular (ref – below)  The transform method is where the custom pipe functionality is implemented, which guides the compiler to have a look-up and execute in run-time  It will accept any number of any type of arguments and return the transformed value which also can be of any type interface PipeTransform { transform(value: any, ...args: any[]): any } Angular official documentation URL: https://angular.io/guide/pipes
  • 31. www.webstackacademy.com Creating a pipe – GUI mode  Step-2: Make your pipe as a part of the module (app.module.ts file) • Add your new pipe (ex: SummaryPipe) under the declarations sections of the module. • Ensure you import appropriate pipe files, similar to components @NgModule({ declarations: [ AppComponent, SummaryPipe ], imports: [ BrowserModule, ], providers: [], bootstrap: [AppComponent] })
  • 32. www.webstackacademy.com Creating a pipe – GUI mode  Step-3: Invoking / Using the pipe  Pipe need to be invoked by the component from its template file <h2>Custom pipe - Example</h2> {{ myText | summarypipe }}  Step-4: Save all and compile. U should be able to use your custom pipe now
  • 34. www.webstackacademy.com Creating a component – CLI mode • Sometimes you might find it little hard to create a component using GUI. In case you miss out any steps, the app will not work. • Angular CLI offers much more reliable way to create a pipe (similar to component and services). Try out the following command, which will not only generate a component but also make appropriate entries $ ng g p myformat // Creates a new pipe
  • 36. www.webstackacademy.com Exercise • Create a custom pipe to find out the zodiac sign of the user • Input is given as an entry in CSV file format as follows: • Here are the Zodiac signs for all the months: Rajan,9845123450,[email protected],22/05/1990,Bangalore • Aries (March 21-April 19) • Taurus (April 20-May 20) • Gemini (May 21-June 20) • Cancer (June 21-July 22) • Leo (July 23-August 22) • Virgo (August 23-September 22) • Libra (September 23-October 22) • Scorpio (October 23-November 21) • Sagittarius (November 22-December 21) • Capricorn (December 22-January 19) • Aquarius (January 20 to February 18) • Pisces (February 19 to March 20)
  • 37. www.webstackacademy.com WebStack Academy #83, Farah Towers, 1st Floor, MG Road, Bangalore – 560001 M: +91-809 555 7332 E: [email protected] WSA in Social Media: