NG表不适用于1条记录

时间:2016-02-24 12:36:03

标签: javascript java jquery html angularjs

我有关于angularJS的问题。

当数据库只有1条记录时,我的ng-table无效。 何时加载2个或更多记录。

浏览器错误控制台 错误:[orderBy:notarray] http://errors.angularjs.org/1.5.0/orderBy/notarray?p0=%7B%22clienteID%22%3A ... clienteNome%22%3A%22Nicolas%22%2C%22clienteConta%22%3A%229.9999999E7%22%7D     在错误(本机)     在

我的HTML:

<table ng-table="tableParams" class="table table-striped" show-filter="true">
                <tr ng-repeat="cliente in data">
                       <td title="'ID'" filter="{ clienteID: 'number'}" sortable="'clienteID'">
                        {{cliente.clienteID}}
                    </td>
                    <td title="'Cliente'" filter="{ clienteNome: 'text'}" sortable="'clienteNome'">
                        {{cliente.clienteNome}}
                    </td>
                    <td><a ng-click="editUser(user.id)" class="btn btn-small btn-primary">Editar</a></td>
                    <td><a ng-click="deleteClienteById(cliente.clienteID)" class="btn btn-small btn-danger">Excluir</a></td>
                </tr>
            </table>

MY CONTROLLER:

angular.module('ProjetoAngularClient').controller('clienteController',
        function($scope,$filter,NgTableParams,clienteService) {

        clienteService.getAllClientes().success(function(response){
        $scope.clientes = response.cliente;
        table();
        });

        function getAll(){
            clienteService.getAllClientes().success(function(response){
                $scope.clientes = null;
                $scope.clientes = response.cliente;
                $scope.tableParams.reload();
                });
        }

          function table(){
                $scope.tableParams = new NgTableParams({
                    page: 1,
                    count:10
                }, {
                    total: $scope.clientes.length, 
                    getData: function ($defer, params) {
                           $scope.data = params.sorting() ? $filter('orderBy')($scope.clientes, params.orderBy()) : $scope.clientes;
                           $scope.data = params.filter() ? $filter('filter')($scope.data, params.filter()) : $scope.data;
                           $scope.data = $scope.data.slice((params.page() - 1) * params.count(), params.page() * params.count());
                           $defer.resolve($scope.data);
                        }
                });  
          };

            $scope.deleteClienteById = function(clienteID) {
            clienteService.deleteClienteById(clienteID);
            getAll();
        };
});

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

在所有情况下都需要一个数组。但是$scope.clientes = response.cliente;$scope.clientes设置为一个客户端。

我不完全理解你的程序逻辑。首先,您似乎从clienteService.getAllClientes()加载,然后通过getAll();致电table()再次加载clienteService.getAllClientes()

快速解决方法是测试response.cliente是否为数组,如果不是,则将其指定为$scope.clientes = [response.cliente];

但是,我认为你的后端应该被修复为总是返回一个数组。