gpt4 book ai didi

angularjs - 有选择地向 ng-repeat AngularJS 添加一个类

转载 作者:行者123 更新时间:2023-12-04 14:13:04 25 4
gpt4 key购买 nike

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

最佳答案

现在只有一个 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>

关于angularjs - 有选择地向 ng-repeat AngularJS 添加一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23618960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com