gpt4 book ai didi

javascript - Angular 表,使用 ngClass 和 ng-repeat 添加类

转载 作者:行者123 更新时间:2023-12-03 07:22:07 24 4
gpt4 key购买 nike

我有一个由 Web 服务 JSON 制成的表,每一行都有一个按钮来标记要删除的行。当您单击行按钮时,JS 警报会显示行元素的 id,我还需要在行上添加“危险” Bootstrap 类。现在,单击按钮时我可以看到行元素 id,并将该 id 添加到列表中,以便稍后将其发送到 Web 服务。

这是我的观点:

<table class="table table-condensed">
<tr>
<th>#</th>
<th><a href="" ng-click="sortField = 'ordre'; reverse = !reverse">Prioritat</a></th>
<th><a href="" ng-click="sortField = 'nomAtribut'; reverse = !reverse">Atribut</a></th>
<th><a href="" ng-click="sortField = 'nomAtribut'; reverse = !reverse">Tipus</a></th>
<th><a href="" ng-click="sortField = 'midaAtribut'; reverse = !reverse">Mida</a></th>
<th><a href="" ng-click="sortField = 'atributObligatori'; reverse = !reverse">Obligatori</a></th>
<th><a href="" ng-click="sortField = 'observacions'; reverse = !reverse">Observacions</a></th>
</tr>
<tr ng-repeat="(key, value) in atrb">
<td>
<a href="" ng-click="alert(value.idatributs_actiu)" ng-model="elimina"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></a>
</td>
<td>
<input type="number" ng-model="value.ordre" value="value.ordre" />
</td>
<td>
<input type="value.valor" ng-model="value.nomAtribut" value="value.nomAtribut" />
</td>
<td>
{{value.valor}}
</td>
<td>
<input type="value.valor" ng-model="value.midaAtribut" value="value.midaAtribut" />
</td>
<td>
<input type="checkbox" ng-model="value.atributObligatori" value="value.atributObligatori" ng-true-value="'Si'" ng-false-value="'No'" />
</td>
<td>
<input type="value.valor" ng-model="value.observacions" value="value.observacions" />
</td>
</tr>

Controller :

$scope.alert = function (index) {
$window.alert('click a borrar id: ' + index); // Show JS alert with id
$scope.addAtributsExistentsEliminar(index); // Add id to array, for later send it to WS
$scope.elimina = true;
$scope.class = 'danger';
}

我一直在尝试使用ngClass来做到这一点和以下 other examples我什么也没得到,甚至连 JS 警报也没有,JS 控制台上也没有显示任何内容。

编辑:

我放置了完整的 Controller 代码:

// Edita tipus d'actius
assets.controller('EditaTipusCtrl', function ($scope, $http, $routeParams, $window) {

$scope.refresh = function () {
$http.get('http://10.0.203.73/WS/ws.php/tipusactius/getDetails/' + $routeParams.id).success(function (data) {
$scope.atrb = data;
});
};

$scope.alert = function (index, rowScope) {
// rowScope.class = 'danger';
$window.alert('click a borrar id: ' + index); // Show JS alert with id
$scope.addAtributsExistentsEliminar(index); // Add id to array, for later send it to WS
$scope.elimina = true;
rowScope.addClass = 'danger';
}

$scope.refresh();

// Construeix combo per definir tipus atributs (String, Date, Text)
$http.get('http://10.0.203.73/WS/ws.php/getCombo/1').success(function (data) {
$scope.options = data;
});

$scope.nousAtributs = [];
$scope.atributsExistentsEliminar = [];

$scope.addNewLine = function () {
var newRow = {
"nomAtribut": "",
"tipus": "",
"mida": '',
"prioritat": "",
"obligatori": "",
"observacions": "",
"nomTipusActiu": $routeParams.id // nom del tipus d'actiu
};
$scope.nousAtributs.push(newRow);
}

$scope.addAtributsExistentsEliminar = function (id) {
$scope.atributsExistentsEliminar.push(id);
}

$scope.showAtributsEliminar = function(){
angular.forEach($scope.atributsExistentsEliminar, $scope.show);
}

$scope.show = function (id) {
$http.get('http://10.0.203.73/WS/ws.php/tipusactius/edita/elimina/' + id + '.json').success(function (data) {
$scope.sts = data.status;
$window.alert($scope.sts);
});

if ($scope.sts.status == 'IN_USE') {
$window.alert('Aquest atribut no es pot eliminar perque és en ús');
}

}

$scope.saveChanges=function(){
angular.forEach($scope.atrb, $scope.sendChanges);
angular.forEach($scope.nousAtributs, $scope.saveNewAttributtes);
$('#myModal').modal('show');
$scope.refresh();
}

$scope.sendChanges=function(atribut){
$http.post('http://10.0.203.73/WS/ws.php/tipusactius/edita', atribut).success(function (data) {
$scope.atrb = data;
});
}

$scope.saveNewAttributtes=function(atribut){
$http.post('http://10.0.203.73/WS/ws.php/tipusactius/edita/nouatribut', atribut).success(function (data){
$scope.atrb = data;
});
}

$scope.removables = function () {

}

});

已解决:

Your current code tries to use the parent scope, which is why it's not working as you expected. You can simply pass in the scope to the alert function. So

$scope.alert = function (index, rowScope) {
...
rowScope.class = 'danger';
}

with your template as

...
<tr ng-repeat="(key, value) in atrb" ng-class="class">
<td>
<a href="" ng-click="alert(value.idatributs_actiu, this)"...

Fiddle - https://jsfiddle.net/y0rtLhyj/

最佳答案

您当前的代码尝试使用父作用域,这就是它无法按您预期工作的原因。您只需将范围传递给 alert 函数即可。所以

$scope.alert = function (index, rowScope) {
...
rowScope.class = 'danger';
}

您的模板为

...
<tr ng-repeat="(key, value) in atrb" ng-class="class">
<td>
<a href="" ng-click="alert(value.idatributs_actiu, this)"...
<小时/>

fiddle - https://jsfiddle.net/y0rtLhyj/

<小时/>

也就是说,正确的方法是在您的 value 对象上添加一些内容来表明它已被删除。然后用它来驱动ng-class。这样你的 Controller 中就没有 View 属性(即class)。

关于javascript - Angular 表,使用 ngClass 和 ng-repeat 添加类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36174442/

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