Add a class selectively to an ng-repeat AngularJS

2019-01-26 11:42发布

问题:

I have an ng-repeat for a table, I want to be able to add a class when <td> is clicked, and remove the class when un-clicked. Multiple <td> can be selected at the same time. Right now ALL of the cities are or are not getting the class applies.

For example: (lets say nodes has 100 items)

<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>

in my 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;
};

回答1:

Right now there is a single clicked property on the scope that you're changing and everything refers to that. Try to put clicked on the node instead...

$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;
};

And in the 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:

You don't need a special function or controller to accomplish this:

<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>

Full Plunker example: http://plnkr.co/edit/1hdcIOfz0nHb91uFWKrv

I could show you the controller I used by it's empty except for the test data. You don't need a function.



回答3:

Alternately, the code can use a separate array and $index to set classes:

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

This approach is useful if you want to separate Model data from View data.

The DEMO

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>