angularJs, ngClass directive vests lots of CSS power in it. Here is one problem ngClass solves so effortlessly.
Problem – highlight selected row in a table when user clicks it, use ngRepeat directive to display all the rows in the table.
Solution
| //Step 1. on ng-click call a method and pass the $index to it. say select($index). |
| //Note - $index is a property of ng-Repeat and represents the index of the selected row |
| <div ng-controller ="MyCtrl"> |
| <tr ng-repeat="car in cars" ng-click="select($index)"> |
| <td>..</td> |
| <tr> |
| </div> |
| //Step2 - On MyCtrl Controller, store the index |
| $scope.select = function(index){ |
| $scope.index = index |
| } |
|
|
| //Step 3- Add ng-Class directive to row , here we are applying the info class in when the index is selected |
| <tr ng-repeat="car in cars" ng-click="select($index)" ng-class="{'info':$index == index}"> |
|
|
| //enjoy |
Comments
Post a Comment