繁体   English   中英

在Angular上的表,添加具有ngClass和ng-repeat的类

[英]Table on Angular, adding classes with ngClass and ng-repeat

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

这是我的看法:

<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其他示例来做到这一点 ,但我什至没有得到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)"... 

小提琴-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)"...

小提琴-https: //jsfiddle.net/y0rtLhyj/


就是说,正确的方法是在您的value对象上包含一些指示其已删除的对象。 然后使用它来驱动ng-class 这样,您的控制器中就没有视图属性(即class )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM