add/remove class of multiple li in angularjs

前端 未结 1 571
遇见更好的自我
遇见更好的自我 2021-01-29 06:40

I have list as below...

  • one
  • two
  • three

N

相关标签:
1条回答
  • 2021-01-29 06:50

    Check the below example:

    var myApp = angular.module('myApp', []);
    myApp.controller('MyCtrl', ['$scope', function($scope) {
      $scope.setMaster = function(section) {
        $scope.selected = section;
      }
    
      $scope.isSelected = function(section) {
        return $scope.selected === section;
      }
    }]);
    .active {
        background-color: orange;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    
    <div ng-app="myApp" ng-controller="MyCtrl">
        <ul>
          <li ng-repeat="i in ['One', 'Two', 'Three']" ng-class="{active : isSelected(i)}">
            <a ng-click="setMaster(i)">{{i}}</a>
          </li>
        </ul>
        <hr> {{selected}}
    </div>

    0 讨论(0)
提交回复
热议问题