I need to add two classes by using ng-class ,one class I am getting by a function another I need to get by an expression - Below is code which I am using but getting some syntax error -
<div class="field-icon" ng-class="getFieldClass(entry.entry_type_id) , 'used': entry_map[entry.guid] > 0" ></div>
What would be correct syntax if it is possible to use function and expression together.
yes , you can do it. Did you try it? What happened? You can use both class and ngClass as the first one gives you the opportunity to apply a class that you want to implement in all cases under any circumstances and the later to apply classes conditionally.
Definition and Usage. The ng-class directive dynamically binds one or more CSS classes to an HTML element. The value of the ng-class directive can be a string, an object, or an array. If it is a string, it should contain one or more, space-separated class names.
ng-style is used to interpolate javascript object into style attribute, not css class. And ng-class directive translates your object into class attribute.
To add a conditional class in Angular we can pass an object to ngClass where key is the class name and value is condition i.e., true or false as shown below. And in the above code, class name will be added only when the condition is true.
Definition of AngularJS ng-class AngularJS ng-class is an In-Built AngularJS directive that is used in the HTML view for designing a web page and using CSS styles which can be handled conditionally and dynamically based on evaluation of an expression.
AngularJS ng-class | How ng-class Directive works in AngularJS? AngularJS ng-class is an In-Built AngularJS directive that is used in the HTML view for designing a web page and using CSS styles which can be handled conditionally and dynamically based on evaluation of an expression.
We can't write an if statement in ngClass, due to the fact that it is a statement. But we can't use a ternary operator since this is an expression. For example, if we want the text in a table cell to have a class of red when its value is larger than 10, and if not it should have a class of green, here is the code:
The Basics When it comes to ngClass, it supports three types of expression "return values": String, Array, and Object. Here is an example from the official documentation: <div [ngClass]="'first second'"> <div [ngClass]=" ['first', 'second']"> <div [ngClass]=" {first: true, second: true, third: true}"> <div [ngClass]=" {'first second': true}">
You had wrong ng-class
syntax, it should be in JSON format like ng-class="{'used': expression2 }"
, expression will return Boolean
on basis of that class will get added or removed from the class attribute
value.
As your getFieldClass
method is returning class name then you could shift your both class logic in getFieldClass
method
Markup
<div class="field-icon"
ng-class="getFieldClass(entry)" ></div>
Code
$scope.getFieldClass = function(entry){
//use entry.entry_type_id here to decide class which is first
//'text-box-icon' class has been selected on some condition based on entry.entry_type_id
return {"text-box-icon": true, 'used': $scope.entry_map[entry.guid] > 0};
}
You can write both function and expression in array.
Example:
<div class="field-icon" ng-class="[getFieldClass(entry.entry_type_id), {'used':entry_map[entry.guid] > 0}]"></div>
Live Example here: https://plnkr.co/edit/OWfkAwJiombrehiS7p9o?p=preview
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With