SlideShare ist ein Scribd-Unternehmen logo
1 von 20
Angular Basics
Angular Directive
In Angular, a directive is a special kind of component that extends the HTML by adding custom
behavior and syntax. Directives allow you to manipulate the Document Object Model (DOM), add
event listeners, and add or remove elements from the DOM.
There are three types of directives in Angular:
 Component Directives: Components are the most common type of directive in Angular and are
used to create reusable UI elements. They have a template, a class, and metadata that describe
how they should be used.
 Structural Directives: Structural directives are used to change the layout of the DOM. Examples
include ngIf, ngFor, and ngSwitch.
 Attribute Directives: Attribute directives are used to change the appearance or behavior of an
element. Examples include ngClass, ngStyle, and ngModel.
Directives are an integral part of Angular and are used to add functionality to your application and
make it more interactive. They allow you to abstract away complex logic and create reusable
components that can be easily shared across your application.
Component Directive
In Angular, a component directive is a reusable UI
element that is defined as a combination of a class, a
template, and metadata that describes how the
component should be used. Components define the
look and behavior of a UI element and can be easily
reused throughout your application.
Here is an example of a component directive in
Angular:
Component Directive
In this example, the component directive is defined using
the @Component decorator, which provides the
metadata for the component. The selector property of
the metadata is used to define a CSS selector that can be
used to include the component in the HTML template of
another component.
The template property defines the HTML template for
the component, and the styles property defines any
styles that should be applied to the component. The
component also has a class, MyComponent, which
defines the data and behavior for the component.
To use the component in your application, you would
include it in the template of another component:
Attribute Directive
In Angular, an attribute directive is a directive that changes the appearance or behavior of an
element. Attribute directives are used to add dynamic behavior to an element, such as changing its
style or handling an event.
Examples include ngClass, ngStyle, and ngModel.
 ngStyle :
In Angular, the ngStyle directive is used to dynamically set styles on an HTML element. It allows you
to bind a JavaScript object to an HTML element's style attribute, so that the styles are automatically
updated whenever the object changes.
Here is an example of using the ngStyle directive in Angular:
Attribute Directive
In this example, the ngStyle directive is used to bind an object with two properties, backgroundColor
and fontSize, to the style attribute of a div element. The values of the backgroundColor and fontSize
properties are used to set the background-color and font-size styles of the element, respectively.
To use this example in your application, you would define the backgroundColor and fontSize
properties in your component and bind them to the ngStyle directive:
To use this example in your application, you would define the backgroundColor and fontSize
properties in your component and bind them to the ngStyle directive:
Attribute Directive
This would render a yellow div element with a font size of 20 pixels. If you later change the values of
the backgroundColor or fontSize properties in your component, the styles of the div element would
be automatically updated to reflect the changes.
Attribute Directive
Attribute Directive
Attribute Directive
In Angular, the ngClass directive is used to dynamically set CSS classes on an HTML element. It allows
you to bind a JavaScript object or an expression to the class attribute of an element, so that the
classes are automatically updated whenever the object or expression changes.
Here is an example of using the ngClass directive with an object in Angular:
Attribute Directive
Attribute Directive
Structural Directive
 NgFor
The ngFor directive is used in Angular to render a list of items in a template. The directive is used with
a * (asterisk) symbol before the keyword ngFor, and it takes an expression that specifies the data
source and the local variable that will be used to reference each item in the list.
Here's an example of how you could use ngFor to display a list of names:
Structural Directive
 ngIf
The ngIf directive in Angular is used to conditionally render elements in the HTML template based on
a boolean expression. It allows you to display or hide an element based on the truthiness of an
expression.
Structural Directive
 ngSwitch
The ngSwitch directive in Angular is used to conditionally render elements in the HTML template
based on a switch expression. It allows you to choose one of several templates to display based on
the value of an expression.
Here is a simple example of how to use the ngSwitch directive:
Custom Directive
 custom Directive
Creating a custom directive in Angular is a simple process. You can use the @Directive decorator to
define a directive, and then you can use it in your HTML template.
Here is an example of a simple custom directive in Angular:
Custom Directive
This directive adds mouse event listeners to the
host element, which is the element the
directive is attached to, and sets the
background color to yellow when the mouse
enters and to null when the mouse leaves. The
directive can be used in your HTML template
like this:
Custom Directive
Parameterized Directive
In Angular, a parameterized directive is a
directive that accepts inputs or parameters
from the user to modify its behavior. This allows
for greater customization and reusability of the
directive.
Questions ?
Thank You

Weitere ähnliche Inhalte

Ähnlich wie Angular Directive Types and Usage Guide

Angular js 1.3 presentation for fed nov 2014
Angular js 1.3 presentation for fed   nov 2014Angular js 1.3 presentation for fed   nov 2014
Angular js 1.3 presentation for fed nov 2014Sarah Hudson
 
Lesson 06 Styles and Templates in WPF
Lesson 06 Styles and Templates in WPFLesson 06 Styles and Templates in WPF
Lesson 06 Styles and Templates in WPFQuang Nguyễn Bá
 
Get Information about Angular Component- Technical Chamber.pdf
Get Information about Angular Component- Technical Chamber.pdfGet Information about Angular Component- Technical Chamber.pdf
Get Information about Angular Component- Technical Chamber.pdfNishaadequateinfosof
 
Angular Js Get Started - Complete Course
Angular Js Get Started - Complete CourseAngular Js Get Started - Complete Course
Angular Js Get Started - Complete CourseEPAM Systems
 
Intoduction to Angularjs
Intoduction to AngularjsIntoduction to Angularjs
Intoduction to AngularjsGaurav Agrawal
 
AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014Ran Wahle
 
angularJs Workshop
angularJs WorkshopangularJs Workshop
angularJs WorkshopRan Wahle
 
Angular interview questions
Angular interview questionsAngular interview questions
Angular interview questionsGoa App
 
Angular Interview Questions & Answers
Angular Interview Questions & AnswersAngular Interview Questions & Answers
Angular Interview Questions & AnswersRatnala Charan kumar
 
introduction to Angularjs basics
introduction to Angularjs basicsintroduction to Angularjs basics
introduction to Angularjs basicsRavindra K
 
Leveling up with AngularJS
Leveling up with AngularJSLeveling up with AngularJS
Leveling up with AngularJSAustin Condiff
 
Angular js interview question answer for fresher
Angular js interview question answer for fresherAngular js interview question answer for fresher
Angular js interview question answer for fresherRavi Bhadauria
 
Dive into Angular, part 1: Introduction
Dive into Angular, part 1: IntroductionDive into Angular, part 1: Introduction
Dive into Angular, part 1: IntroductionOleksii Prohonnyi
 

Ähnlich wie Angular Directive Types and Usage Guide (20)

Angular js 1.3 presentation for fed nov 2014
Angular js 1.3 presentation for fed   nov 2014Angular js 1.3 presentation for fed   nov 2014
Angular js 1.3 presentation for fed nov 2014
 
Angular Directives
Angular DirectivesAngular Directives
Angular Directives
 
Lesson 06 Styles and Templates in WPF
Lesson 06 Styles and Templates in WPFLesson 06 Styles and Templates in WPF
Lesson 06 Styles and Templates in WPF
 
Get Information about Angular Component- Technical Chamber.pdf
Get Information about Angular Component- Technical Chamber.pdfGet Information about Angular Component- Technical Chamber.pdf
Get Information about Angular Component- Technical Chamber.pdf
 
Angular Js Get Started - Complete Course
Angular Js Get Started - Complete CourseAngular Js Get Started - Complete Course
Angular Js Get Started - Complete Course
 
Intoduction to Angularjs
Intoduction to AngularjsIntoduction to Angularjs
Intoduction to Angularjs
 
AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014
 
angularJs Workshop
angularJs WorkshopangularJs Workshop
angularJs Workshop
 
Angular js
Angular jsAngular js
Angular js
 
Angular interview questions
Angular interview questionsAngular interview questions
Angular interview questions
 
Angular.pptx
Angular.pptxAngular.pptx
Angular.pptx
 
Angular2
Angular2Angular2
Angular2
 
Angular Interview Questions & Answers
Angular Interview Questions & AnswersAngular Interview Questions & Answers
Angular Interview Questions & Answers
 
introduction to Angularjs basics
introduction to Angularjs basicsintroduction to Angularjs basics
introduction to Angularjs basics
 
Leveling up with AngularJS
Leveling up with AngularJSLeveling up with AngularJS
Leveling up with AngularJS
 
Angular js interview question answer for fresher
Angular js interview question answer for fresherAngular js interview question answer for fresher
Angular js interview question answer for fresher
 
Angularjs Basics
Angularjs BasicsAngularjs Basics
Angularjs Basics
 
Dive into Angular, part 1: Introduction
Dive into Angular, part 1: IntroductionDive into Angular, part 1: Introduction
Dive into Angular, part 1: Introduction
 
AngularJS in practice
AngularJS in practiceAngularJS in practice
AngularJS in practice
 
Angular js
Angular jsAngular js
Angular js
 

Kürzlich hochgeladen

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 

Kürzlich hochgeladen (20)

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 

Angular Directive Types and Usage Guide

  • 2. Angular Directive In Angular, a directive is a special kind of component that extends the HTML by adding custom behavior and syntax. Directives allow you to manipulate the Document Object Model (DOM), add event listeners, and add or remove elements from the DOM. There are three types of directives in Angular:  Component Directives: Components are the most common type of directive in Angular and are used to create reusable UI elements. They have a template, a class, and metadata that describe how they should be used.  Structural Directives: Structural directives are used to change the layout of the DOM. Examples include ngIf, ngFor, and ngSwitch.  Attribute Directives: Attribute directives are used to change the appearance or behavior of an element. Examples include ngClass, ngStyle, and ngModel. Directives are an integral part of Angular and are used to add functionality to your application and make it more interactive. They allow you to abstract away complex logic and create reusable components that can be easily shared across your application.
  • 3. Component Directive In Angular, a component directive is a reusable UI element that is defined as a combination of a class, a template, and metadata that describes how the component should be used. Components define the look and behavior of a UI element and can be easily reused throughout your application. Here is an example of a component directive in Angular:
  • 4. Component Directive In this example, the component directive is defined using the @Component decorator, which provides the metadata for the component. The selector property of the metadata is used to define a CSS selector that can be used to include the component in the HTML template of another component. The template property defines the HTML template for the component, and the styles property defines any styles that should be applied to the component. The component also has a class, MyComponent, which defines the data and behavior for the component. To use the component in your application, you would include it in the template of another component:
  • 5. Attribute Directive In Angular, an attribute directive is a directive that changes the appearance or behavior of an element. Attribute directives are used to add dynamic behavior to an element, such as changing its style or handling an event. Examples include ngClass, ngStyle, and ngModel.  ngStyle : In Angular, the ngStyle directive is used to dynamically set styles on an HTML element. It allows you to bind a JavaScript object to an HTML element's style attribute, so that the styles are automatically updated whenever the object changes. Here is an example of using the ngStyle directive in Angular:
  • 6. Attribute Directive In this example, the ngStyle directive is used to bind an object with two properties, backgroundColor and fontSize, to the style attribute of a div element. The values of the backgroundColor and fontSize properties are used to set the background-color and font-size styles of the element, respectively. To use this example in your application, you would define the backgroundColor and fontSize properties in your component and bind them to the ngStyle directive: To use this example in your application, you would define the backgroundColor and fontSize properties in your component and bind them to the ngStyle directive:
  • 7. Attribute Directive This would render a yellow div element with a font size of 20 pixels. If you later change the values of the backgroundColor or fontSize properties in your component, the styles of the div element would be automatically updated to reflect the changes.
  • 10. Attribute Directive In Angular, the ngClass directive is used to dynamically set CSS classes on an HTML element. It allows you to bind a JavaScript object or an expression to the class attribute of an element, so that the classes are automatically updated whenever the object or expression changes. Here is an example of using the ngClass directive with an object in Angular:
  • 13. Structural Directive  NgFor The ngFor directive is used in Angular to render a list of items in a template. The directive is used with a * (asterisk) symbol before the keyword ngFor, and it takes an expression that specifies the data source and the local variable that will be used to reference each item in the list. Here's an example of how you could use ngFor to display a list of names:
  • 14. Structural Directive  ngIf The ngIf directive in Angular is used to conditionally render elements in the HTML template based on a boolean expression. It allows you to display or hide an element based on the truthiness of an expression.
  • 15. Structural Directive  ngSwitch The ngSwitch directive in Angular is used to conditionally render elements in the HTML template based on a switch expression. It allows you to choose one of several templates to display based on the value of an expression. Here is a simple example of how to use the ngSwitch directive:
  • 16. Custom Directive  custom Directive Creating a custom directive in Angular is a simple process. You can use the @Directive decorator to define a directive, and then you can use it in your HTML template. Here is an example of a simple custom directive in Angular:
  • 17. Custom Directive This directive adds mouse event listeners to the host element, which is the element the directive is attached to, and sets the background color to yellow when the mouse enters and to null when the mouse leaves. The directive can be used in your HTML template like this:
  • 18. Custom Directive Parameterized Directive In Angular, a parameterized directive is a directive that accepts inputs or parameters from the user to modify its behavior. This allows for greater customization and reusability of the directive.