【问题标题】:add/remove class of multiple li in angularjs在angularjs中添加/删除多个li的类
【发布时间】:2017-01-17 14:05:49
【问题描述】:

我的清单如下...

<ul id="menu">
  <li>one</li>
  <li>two</li>
  <li>three</li>
</ul>

现在,当单击特定的 li 时,我希望将 active 类添加到同一类中,并从其余的 li 元素中删除 active 类。此外,当再次单击相同的 li 时,我想删除 active 类。

如何,我可以使用ng-clickng-class 做到这一点吗?

【问题讨论】:

标签: angularjs ng-class


【解决方案1】:

查看以下示例:

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>

【讨论】:

    猜你喜欢
    • 2018-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-06
    • 2014-10-01
    • 2014-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多