【问题标题】:How do I change the class of each individual div in a series of divs when clicked on?单击时如何更改一系列 div 中每个单独 div 的类?
【发布时间】:2014-09-10 05:44:40
【问题描述】:

我正在尝试在单击单个 div 时更改它的类,而我所拥有的却不起作用。我无法理解 ng-click 和 ng-class 如何协同工作,这就是为什么我无法弄清楚这一点的原因。我需要做什么或在我的 changeBackground 函数中放置什么,以便单个 div 应用一个类?

<div ng-controller="BoardController">
    <div class="rows" ng-repeat="r in rows track by $index" ng-click="changeBackground($index)" ng-class="{ red: }">

    </div>
</div>

$scope.changeBackground = function(index) {

};

【问题讨论】:

标签: angularjs


【解决方案1】:

试试:

<div ng-controller="BoardController">
    <div class="rows" ng-repeat="r in rows track by $index"
            ng-click="r.clicked = !r.clicked" ng-class="{ 'red':r.clicked }">

    </div>
</div>

【讨论】:

  • 与其在 html 中使用条件语句,不如在我的示例中使用在 ng-click 上调用的后台 javascript 函数来完成?
  • 我在此页面底部找到了我要查找的内容stackoverflow.com/questions/14545744/…
猜你喜欢
  • 1970-01-01
  • 2013-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-08
  • 2018-12-07
  • 2017-04-03
  • 2019-04-10
相关资源
最近更新 更多