styleUrl
property in the @Component
decorator links to an external CSS file.
Angular automatically fetches and applies these styles to the component.styleUrl
property points to ./app.component.css
, which contains the CSS styles for this component.div
elements with different class names (primary
, danger
, and success
). These classes will be styled according to the CSS rules defined in app.component.css
.warning
message in the template with a different color (i.e. orange
)styles
property.`
since it supports multi-line strings.info
message in the template with a different color (i.e. blue
)div
element above is a powerful feature called class binding.true
, the xyz
class is applied to the element.div
element and three buttons. The div
always use the alert
class and conditionally applies the primary
, danger
, or success
class based on the alert.type
value.info
alert colored in blue
[attribute]
to dinamically assign a CSS class to the element.
This approach can be extremely useful when the business logic for assigning the class is complex.[class]
attribute to the method.
But this is not a good practice, as I explained in the Signal chapters, since we can have performance issues.computed
, to evaluate and define the class to apply only when signal changes.computed
(optimized)getMessage
computed property and return a different message in according to the alert().type
.
Use this new computed
property to display the message in the template instead to manually set
its value when buttons are clickedgetCls()
method because it is used in the template binding ([class]="getCls()"
).getCls()
involves a switch statement and string concatenation behind the scenes.ngClass
directive conditionally applies CSS classes to a HTML element based on the value of a conditionimports
decorator property in order to be used in templatengClass
is an Angular directive that allows you to dynamically add or remove CSS classes on an element.warning
messagengClass
directive allows you to bind an element's class attribute to a condition or a set of conditions.ngClass
can take a string
, an array
, or an object
to determine which classes are applied.alert
and primary
classes to the element.cls1
and cls2
classes to the element.success
, primary
, or danger class based on the alert.type
value
.ngClass
color
and background-color
using the alert()
signal directly in the template, as we did in this example, create and use 2 new computed
properties instead:bgColor
color