我有一个由Web Service JSON构成的表,每行都有一个按钮来标记要删除的行。当你点击行按钮时JS报警显示行元素的id,我还需要在行上添加'danger'引导类。现在,我可以在单击按钮时看到行元素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>
控制器:
$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控制台上。
编辑:
我把完整的控制器代码:
// 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 () {
}
});
解决:
您当前的代码尝试使用父作用域,这就是原因 像你期望的那样工作。您只需将范围传递给警报即可 功能。所以
$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)"...
答案 0 :(得分:1)
您当前的代码尝试使用父作用域,这就是它无法按预期工作的原因。您只需将范围传递给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)"...
小提琴 - https://jsfiddle.net/y0rtLhyj/
那就是说,正确的方法是在你的value
对象上有一些东西,表明它被删除了。然后用它来驱动ng-class
。这样,您的控制器中就没有视图属性(即class
)。