【问题标题】:Add a class selectively to an ng-repeat AngularJS有选择地向 ng-repeat AngularJS 添加一个类
【发布时间】:2014-05-12 21:11:43
【问题描述】:

我有一个表格的 ng-repeat,我希望能够在单击 <td> 时添加一个类,并在未单击时删除该类。可以同时选择多个<td>。现在所有的城市都在申请或没有申请。

例如:(假设节点有 100 个项目)

<tr ng-repeat node in nodes>
  <td>{{node.name}}</td>
  <td>{{node.date}}</td>
  <td ng-click="toggleMe( node.city )" ng-class"{clicked : isClicked()}" >{{node.city}}</td>
</tr>

在我的 JS 中

$scope.cityArr = [];

$scope.toggleMe = function(city) {
  if ($scope.count > 0) {
    angular.forEach($scope.cityArr, function(value) {
      if (city === value) {
        $scope.clicked = false;
      } else {
        $scope.cityArr.push(city);
        $scope.clicked = true;
      }
    });
  } else {
    $scope.cityArr.push(city);
    $scope.clicked = true;
  }
  $scope.count = 1;
};

$scope.isClicked = function() {
  return $scope.clicked;
};

【问题讨论】:

    标签: angularjs ng-class angularjs-ng-class


    【解决方案1】:

    现在,您正在更改的范围内有一个 clicked 属性,所有内容都指向该属性。尝试将clicked 放在节点上...

    $scope.toggleMe = function(node) {
      if ($scope.count > 0) {
        angular.forEach($scope.cityArr, function(value) {
          if (node.city === value) {
            node.clicked = false;
          } else {
            $scope.cityArr.push(node.city);
            node.clicked = true;
          }
        });
      } else {
        $scope.cityArr.push(node.city);
        node.clicked = true;
      }
      $scope.count = 1;
    };
    

    ngRepeat...

    <tr ng-repeat node in nodes>
      <td>{{node.name}}</td>
      <td>{{node.date}}</td>
      <td ng-click="toggleMe( node )" ng-class"{clicked : node.clicked}" >{{node.city}}</td>
    </tr>
    

    【讨论】:

      【解决方案2】:

      您不需要特殊的功能或控制器来完成此操作:

      <table>
          <tbody>
              <tr ng-repeat="node in nodes">
                  <td>{{node.name}}</td>
                  <td>{{node.date}}</td>
                  <td ng-click="node.highlight = !node.highlight" 
                      ng-class="{ highlight: node.highlight }">
                      {{node.city}}
                  </td>
              </tr>
          </tbody>
      </table>
      

      完整的 Plunker 示例:http://plnkr.co/edit/1hdcIOfz0nHb91uFWKrv

      我可以告诉你我使用的控制器是空的,除了测试数据。你不需要函数。

      【讨论】:

        【解决方案3】:

        或者,代码可以使用单独的数组和$index 来设置类:

        <tr ng-repeat="node in nodes"
            ng-class="{ highlight: highlightRows[$index] }">
          <td class="x" ng-click="toggleHighlight($index)">
            X
          </td>
        

        如果您想将模型数据与视图数据分开,这种方法很有用。

        演示

        angular.module("app", [])
        .controller("TestController", function($scope) {
          $scope.highlightRows = [];
          $scope.toggleHighlight = function(idx) {
              $scope.highlightRows[idx] = !$scope.highlightRows[idx];
          };
          $scope.nodes = [
            { name: "Alpha", date: new Date(), city: "Omaha" },
            { name: "Bravo", date: new Date(), city: "New York" },
            { name: "Charlie", date: new Date(), city: "Minneapolis" }
          ];
        })
        table {
          border-collapse: collapse;
          font-family: sans-serif;
        }
        td {
          padding: 5px;
          border: solid black 1px;
        }
        .x {
          cursor: pointer;
        }
        .highlight {
          background: yellow;
        }
        <script src="//unpkg.com/angular/angular.js"></script>
        <body ng-app="app" ng-controller="TestController">
            <table>
             <h3>Click on X to highlight</h3>
              <tbody>
                <tr ng-repeat="node in nodes"
                    ng-class="{ highlight: highlightRows[$index] }">
                  <td class="x" ng-click="toggleHighlight($index)">
                    X
                  </td>
                  <td>{{node.name}}</td>
                  <td>{{node.date | date}}</td>
                  <td>{{node.city}}</td>
                </tr>
              </tbody>
            </table>
            highlightRows={{highlightRows}}
        </body>

        【讨论】:

        • 谢谢@georgeawg!
        猜你喜欢
        • 1970-01-01
        • 2017-06-24
        • 2018-08-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-11
        • 1970-01-01
        • 2014-02-16
        相关资源
        最近更新 更多