6

我有一个表的 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;
};
4

3 回答 3

4

现在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>
于 2014-05-12T21:22:16.120 回答
4

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

<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

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

于 2014-05-12T21:27:47.773 回答
1

或者,代码可以使用单独的数组和$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>

于 2018-08-01T15:23:18.233 回答