【问题标题】:Table on Angular, adding classes with ngClass and ng-repeatAngular 上的表格,使用 ngClass 和 ng-repeat 添加类
【发布时间】:2016-03-23 21:53:17
【问题描述】:

我有一个由 Web 服务 JSON 制成的表格,每一行都有一个按钮来标记要删除的行。当您单击行按钮时,JS 警报会显示行元素的 id,我还需要在行上添加“危险”引导类。现在我可以在单击按钮时看到行元素 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)"...

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

【问题讨论】:

标签: javascript angularjs twitter-bootstrap angularjs-ng-repeat ng-class


【解决方案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)。

【讨论】:

  • 我做到了,但我在控制台上收到此错误:'Cannot set property 'class' of undefined'
  • 你能把你的 HTML 和脚本粘贴到我的 fiddle 中,fork 并发布链接吗?
  • 我改了:jsfiddle.net/hqu4th4z 但不知道为什么在 jsfiddle 上有效,但在我的服务器上无效
  • 我发布了完整的控制器
  • 在您发布的小提琴中,您在控制器中使用addClass,在模板中使用class
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-05
  • 2016-02-14
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 2023-03-04
  • 2013-03-02
相关资源
最近更新 更多