Recently in the learning angular framework, ng-class is an instruction in the angular framework, and here is the official explanation of the Ng-class directive:
The ng-class directive is used to dynamically bind one or more CSS classes to an HTML element.
ng-class
The value of the directive can be a string, an object, or an array.
If it is a string, multiple class names are separated by spaces.
If it is an object, you need to use the key-value pair, and key is a Boolean value, which is the class name you want to add. The class is added only if key is true.
If it is an array, it can consist of a string or an object combination, and the elements of the array can be strings or objects.
There are three ways to handle class in angular:
1:scope variable bindings.
2: string array form.
3: Object Key/value processing.
Example 1: (Scope variable binding form)
function Test ($scope) { $scope. Test = "classname";} < class= "{{test}}"></div>
It is not recommended in this way.
Example 2: (String array form)
function Ctr ($scope) { $scope. isActive = true;} < ng-class= "{true: ' active ', false: ' inactive '}[isactive]"></ Div >
This code means that when the value of IsActive is true, the dynamically bound class is active, and for Flase, dynamically bound classes are inactive, which can be separated by a space when multiple class names need to be bound.
Example 3: (Object Key/value processing)
You need to use Key-value pair, key is a Boolean value, and value is the class name you want to add. The class is added only if key is true.
function Ctr ($scope) {} < ng-class {' selected ': isSelected, ' car ': IsCar} '></div>
In fact, these and arrays are similar in string mode, when IsSelected is true, the Select class is dynamically bound, and when Iscar is true, the car class is bound dynamically.
The use of Ng-class