33
loading...
This website collects cookies to deliver better user experience
directive
.Directives are classes that add additional behavior to elements in your Angular applications
ul li
is used to show some text in bullet form. Eg.<ul>
<li>83</li>
<li>32</li>
<li>66</li>
</ul>
attributeDirectiveDemo
. If you are not familiar about angular component or how to create one, I would suggest you to go through the below link -.success {
color: green;
}
.error {
color: red;
}
success
and error
. When success class is used it will color the text green. While the error class will color the text with red.<div [ngClass]="'success'">Server One</div>
<div [ngClass]="'error'">Server Two</div>
[]
Square Bracket. The directive name is placed inside the square bracket. Then the equal =
operator comes followed by the class name. The class name success
/ error
is enclosed in double quotes "
and single quote '
. <div [ngClass]='"success"'>Server One</div>
<div [ngClass]="success">Server One</div>
[ngClass]="{ <class_name>: <condition> }"
<div [ngClass]="{ success: serverOneStatus === 'up' }">Server One</div>
serverOneStatus
has a value of up (Remember we initialized the variable serverOneStatus in the TS file with the value up) i.e. the condition evaluates to true then the corresponding CSS class success
will be applied else the CSS class wont be applied.[ngClass]="{
<Class_1>: <Condition_1>,
<Class_2>: <Condition_2>
}"
,
. Depending on which condition evaluates to true the corresponding class will be applied. If both the condition results to true then both the class will be applied.<div
[ngClass]="{
success: serverOneStatus === 'up',
error: serverOneStatus === 'down'
}"
>
Server One
</div>
error: serverOneStatus === 'down'
serverOneStatus
has a value up
then the success class will be appended and if the variable holds a value down
then the error class will be appended to the element on which ngClass is used, here in this case div
.[ngClass]="<codition> ? <'Class_Name1'> : <'Class_Name2'>"
<div [ngClass]="serverOneStatus == 'up' ? 'success' : 'error'">Server One</div>
serverOneStatus == 'up'
evaluates to true then the success class will be appended else error.