Built-in
Attribute directives listen to and modify the behavior of other HTML elements, attributes, properties, and components.
The most common attribute directives are as follows:
NgClass
1. Using ngClass
with a Simple Condition
ngClass
with a Simple ConditionSuppose you have a component and you want to toggle a CSS class based on a boolean condition.
HTML:
Component (TypeScript):
CSS:
Result:
If isHighlighted
is true
, the div will have a yellow background due to the highlight
class being applied:
If isHighlighted
is false
, the div will have no special styling applied and will appear as default.
2. Using ngClass
with Multiple Conditions
ngClass
with Multiple ConditionsYou can apply multiple CSS classes based on different conditions.
HTML:
Component (TypeScript):
CSS:
Result:
The div will have a yellow background (due to highlight
), and italic text (due to italic
). It will not be bold because isBold
is false
:
3. Using ngClass
with an Array of Classes
ngClass
with an Array of ClassesIf you want to apply a list of classes based on a condition, you can use an array.
HTML:
Component (TypeScript):
CSS:
Result:
The div will have a yellow background due to highlight
being true
. It will not be bold because isBold
is false
:
4. Using ngClass
with an Object
You can apply CSS classes based on an object defined in the component.
HTML:
Component (TypeScript):
CSS:
Result:
The div will have a yellow background due to highlight
being true
. It will not be bold because isBold
is false
:
5. Combining ngClass
with ngStyle
ngClass
with ngStyle
You can combine ngClass
with ngStyle
to apply both CSS classes and dynamic styles.
HTML:
Component (TypeScript):
CSS:
Result:
The div will have a yellow background due to highlight
being true
and will have a font size of 20px
:
Summary
ngClass
is a powerful Angular directive for dynamically applying CSS classes based on conditions.Usage options include: objects with conditions, arrays of classes, and methods returning class objects.
Combining with
ngStyle
allows for dynamic styling along with class application.
NgStyle
Scenario:
Let's say you have a component where you want to dynamically change the background color, font size, and text color of a div
based on certain conditions or user input.
1. Component TypeScript:
Create a component that defines the dynamic styles.
TypeScript (Component):
2. Component HTML:
Use ngStyle
to apply dynamic styles to the div
.
HTML:
3. Component CSS (Optional):
You may have some CSS styles if needed, but in this case, they are not strictly necessary.
CSS:
Result:
The
div
will have its background color, font size, and text color dynamically set according to the values defined in the component.Clicking the buttons will update the properties (
fontSize
,backgroundColor
,textColor
) and, in turn, update the inline styles of thediv
accordingly.
Rendered HTML:
Initially, the div
will look like this:
After Interactions:
Clicking "Toggle Highlight" will toggle the
isHighlighted
flag (though it's not directly used here, you could incorporate it in the style changes).Clicking "Increase Font Size" will set the
fontSize
to30px
.Clicking "Decrease Font Size" will set the
fontSize
to15px
.Clicking "Change Background Color" will change the
backgroundColor
tolightgreen
.Clicking "Change Text Color" will change the
textColor
tored
.
As a result of these interactions, the styles applied to the div
will dynamically update based on the properties and methods defined in the component.
Example after button clicks:
Summary
ngStyle
is used to apply dynamic inline styles to HTML elements based on component properties or methods.Dynamic Styles can be adjusted interactively or based on any logic within your component, providing a flexible way to manage inline styles.
NgModel
Scenario:
Let's create a component where you use ngModel
to bind an input field to a component property. We will also include a button to update the component property, and this change will automatically reflect in the input field.
1. Component TypeScript:
Define the properties and methods for binding and updating in the component.
TypeScript (Component):
2. Component HTML:
Use ngModel
to bind an input field to the userName
property. Include a button to update the userName
.
HTML:
3. Component CSS (Optional):
You may include some CSS for styling, but it’s not essential for this example.
CSS:
Result:
Initial State: The input field will be pre-filled with the value
'John Doe'
, and the paragraph below will display'Your name is: John Doe'
.
Rendered HTML:
Initially:
User Interaction: Typing in the input field will immediately update the
userName
property in the component and vice versa.Button Clicks: Clicking "Change Name to Alice" or "Change Name to Bob" will update the
userName
property and automatically reflect the changes in both the input field and the paragraph.
After Interaction:
For example, after clicking "Change Name to Alice":
Summary
ngModel
creates a two-way data binding between the input field and the component property.Changes in the input field automatically update the component property and vice versa.
User interactions with input fields and buttons show how
ngModel
synchronizes the view and model seamlessly.
Last updated