Logo

Is there any way to make an expression for something like ng-class to be a conditional?

Yes! AngularJS allows quite a bit of flexibility with the ng-class directive. You can use object notation, array notation, or ternary expressions inside ng-class to conditionally set CSS classes. Below are two common patterns.

1. Conditional Classes with Object Notation

You can pass an object literal to ng-class where the key is the class name and the value is a boolean expression. If the boolean is true, the class is applied; if it’s false, it’s removed.

<div ng-class="{ 'active': isSelected, 'disabled': !isEnabled }"> Conditional Class Example </div>
  • 'active': isSelected: If isSelected is true, the <div> gets the active class.
  • 'disabled': !isEnabled: If isEnabled is false, the <div> gets the disabled class.

You can add any valid AngularJS expression as the value, which means you can combine multiple conditions:

<div ng-class="{ 'highlight': (userRole === 'admin' && item.urgent), 'faded': (item.status === 'archived') }"> Another Conditional Example </div>

2. Ternary Expressions

If you only want to pick one class among alternatives, the ternary operator (condition ? classIfTrue : classIfFalse) works well:

<div ng-class="isOnline ? 'online' : 'offline'"> Ternary Example </div>

Depending on isOnline, AngularJS will apply online or offline class to this <div>.

You can also chain multiple conditions. For example, if you have a priority check:

<div ng-class=" priority === 'high' ? 'alert-high' : (priority === 'medium' ? 'alert-medium' : 'alert-low') "> Chained Ternary Example </div>

While this works, keep in mind readability—sometimes object notation is clearer if you have many conditions.

Best Practices

  1. Keep It Readable
    For multiple complex conditions, object notation is often more readable than deeply nested ternaries.

  2. Expression Simplicity
    If your logic becomes too involved, consider moving it into a controller function:

    // In Controller $scope.getItemClasses = function(item) { return { highlight: item.isActive && !item.read, error: item.hasError, }; };
    <!-- In Template --> <div ng-class="getItemClasses(item)">Item Details</div>

    This keeps the template tidy and logic in your controller.

  3. Performance Considerations
    AngularJS sets up watchers for these expressions, so very large or deeply nested conditions may slow performance in huge lists. Use one-time bindings (::) or track by id in ng-repeat for better performance when possible.

Going Beyond: Strengthen Your JavaScript & System Design Foundations

Managing conditional classes is just one piece of building robust front-end applications. If you want to excel in AngularJS (and modern JS frameworks), a strong grasp of JavaScript fundamentals and system design is crucial. Here are some resources from DesignGurus.io to level up your skills:

If you’d like personalized feedback, consider Coding Mock Interviews or System Design Mock Interviews with ex-FAANG engineers. You can also find free tutorials on coding and system design on the DesignGurus.io YouTube Channel.

Final Thoughts

Yes, you can absolutely write a conditional expression inside ng-class. Whether you choose object notation or ternary expressions depends on how many conditions you have and how readable you want your template to be. By combining these techniques with broader AngularJS and JavaScript best practices, you’ll create more maintainable, dynamic UIs that enhance the user experience.

CONTRIBUTOR
TechGrind