Bacancy Technology
Bacancy Technology represents the connected world, offering innovative and customer-centric information technology experiences, enabling Enterprises, Associates and the Society to Rise™.
12+
Countries where we have happy customers
1050+
Agile enabled employees
06
World wide offices
12+
Years of Experience
05
Agile Coaches
14
Certified Scrum Masters
1000+
Clients projects
1458
Happy customers
Artificial Intelligence
Machine Learning
Salesforce
Microsoft
SAP
August 23, 2023
The NgClass directive allows you to set the CSS class dynamically for a DOM element. There are two ways to use this directive,
1. The first is by passing an object literal to the directive
[ngClass]="{'your_class_name': true}"
When using an object literal, the keys are the classes that are added to the element if the value of the key evaluates to true. So in the above example, since the value is true this will set the class onto the element the directive is attached to. Refer is the example,
[ngClass]="{ 'text-success': country === 'UK', 'text-primary': country === 'USA', 'text-danger': country === 'HK' }"
We can also set a class on an element by binding to the input property binding called class , like
[class]="'your_class_name'"
If we want just to add the list of classes already set on the element, we can use the following extended syntax
[class.your_class_name]="true"
following is the best example with multiple class binding
[class.text-success]="person.country === 'UK'" [class.text-primary]="person.country === 'USA'" [class.text-danger]="person.country === 'HK'"