,

AngularJS : Applying CSS class using ng-class directive

Posted by


To assign CSS classes to elements in an array using ng-class in AngularJS, you need to follow a few steps. This tutorial will walk you through the process, from setting up your AngularJS project to applying CSS classes dynamically based on the elements in the array.

Step 1: Setting up your AngularJS project
Before you can start using ng-class to assign CSS classes to elements in an array, you need to set up your AngularJS project. This involves creating a new AngularJS application, defining a controller, and including the necessary AngularJS scripts in your HTML file.

To create a new AngularJS application, you can use the following code:

<!DOCTYPE html>
<html ng-app="myApp">
<head>
  <title>Array: Assign CSS class using ng-class in AngularJS</title>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
</head>
<body ng-controller="myCtrl">
</body>
</html>

In this code snippet, we’ve included the AngularJS script from a CDN and defined an AngularJS application named ‘myApp’. We will also define a controller named ‘myCtrl’ later in this tutorial.

Step 2: Defining the controller
Next, you need to define the controller in your AngularJS application. The controller will be responsible for managing the data and logic related to the elements in the array.

<script>
angular.module('myApp', [])
  .controller('myCtrl', function($scope) {
    $scope.items = [
      {name: 'Item 1', isActive: true},
      {name: 'Item 2', isActive: false},
      {name: 'Item 3', isActive: true}
    ];
  });
</script>

In this code snippet, we’ve defined a controller named ‘myCtrl’ and initialized an array named ‘items’ with three objects. Each object represents an item in the array and has two properties: ‘name’ and ‘isActive’. The ‘isActive’ property will determine whether or not a CSS class should be applied to the corresponding element in the array.

Step 3: Applying ng-class directive
Now that we have our controller set up and our data defined, we can apply the ng-class directive to dynamically assign CSS classes to elements in the array based on the value of the ‘isActive’ property.

<body ng-controller="myCtrl">
  <ul>
    <li ng-repeat="item in items" ng-class="{active: item.isActive}">
      {{ item.name }}
    </li>
  </ul>
</body>

In this code snippet, we’re using the ng-repeat directive to iterate over the ‘items’ array and create a list item for each item in the array. We’ve also applied the ng-class directive to each list item, with the following syntax: {classname: expression}. The expression determines whether or not the specified CSS class should be applied to the element.

In this case, we’re using the ‘active’ class and setting the expression to ‘item.isActive’. If the ‘isActive’ property of an item is true, the ‘active’ class will be applied to the corresponding list item.

Step 4: Styling the CSS classes
Finally, you can define your CSS classes in your stylesheet to style the elements based on the classes applied dynamically with ng-class.

<style>
.active {
  color: green;
  font-weight: bold;
}
</style>

In this code snippet, we’ve defined the ‘active’ class in our CSS stylesheet with a green text color and bold font weight. This styling will be applied to any list items that have the ‘active’ class assigned dynamically using ng-class.

And that’s it! You’ve successfully assigned CSS classes to elements in an array using ng-class in AngularJS. You can customize this tutorial further by adding more properties to your data objects and applying additional CSS classes based on different conditions. Experiment with different expressions and CSS styles to achieve the desired effect in your AngularJS application.

0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x