Add a class selectively to an ng-repeat AngularJS

前端 未结 3 998
天命终不由人
天命终不由人 2020-12-20 21:27

I have an ng-repeat for a table, I want to be able to add a class when is clicked, and remove the class when un-clicked. Multiple

3条回答
  •  萌比男神i
    2020-12-20 21:54

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

    
      {{node.name}}
      {{node.date}}
      {{node.city}}
    
    

提交回复
热议问题