ng-Table在重新加载请求时不呈现新数据

pal*_*ani 3 angularjs ngtable

我有ng-table页面,可以正常使用初始数据.我有一个选择框,根据所选的选项发送服务器请求,然后我将新数据转换为角度,但不用ng-table更新.

这是我的观点:

<section data-ng-controller="ReportsController">
<div class="plain-box">
    <table ng-table="tableParams" show-filter="true" class="table table-striped">
        <thead>
        <tr>
            <th ng-repeat="column in columns" ng-show="column.visible"
                class="text-center sortable" ng-class="{
                    'sort-asc': tableParams.isSortBy(column.field, 'asc'),
                    'sort-desc': tableParams.isSortBy(column.field, 'desc')
                  }"
                ng-click="tableParams.sorting(column.field, tableParams.isSortBy(column.field, 'asc') ? 'desc' : 'asc')">
                {{column.title}}
            </th>
        </tr>
        </thead>
        <tbody>
        <tr ng-repeat="user in $data">
            <td ng-repeat="column in columns" sortable="column.field">
                {{user[column.field]}}
            </td>
        </tr>
        </tbody>
    </table>
</div>
Run Code Online (Sandbox Code Playgroud)

我的控制器:

angular.module('reports').controller('ReportsController', ['$scope', '$stateParams', '$location', 'Authentication', 'Reports', '$filter', 'ngTableParams',
function($scope, $stateParams, $location, Authentication, Reports, $filter, ngTableParams ) {
    $scope.reportBillingPeriod = 0;
    $scope.$watch('reportBillingPeriod', function(newValue, oldValue) {
        $scope.findReportData( newValue );
    });
    //$scope.reportBillingData = 0;


    $scope.findReportData = function( selectedMonth ){
        var selectedPeriod = selectedMonth;
        if( selectedPeriod === null )
        selectedPeriod = '0';
        $scope.customers_report = Reports.customer_report({
        monthReport: selectedPeriod
        }, function( result ){
        var data = result.customers;
        $scope.columns = [
            { title: 'Account Name', field: 'accountName', visible: true, filter: { 'name': 'text' } },
            { title: 'Gross Revenue', field: 'planTotalAmount', visible: true }
        ];

   $scope.$watch('result', function () {
        $scope.tableParams.settings().$scope = $scope;
        $scope.tableParams.reload(); 
    });

        $scope.tableParams = new ngTableParams({
            page: 1,            // show first page
            count: 10,          // count per page
            filter: {
            name: 'O'       // initial filter
            }
        }, {
            total: data.length, // length of data
            getData: function($defer, params) {
            var orderedData = params.sorting() ? $filter('orderBy')(data, params.orderBy()) : data;
            $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
            //$scope.reportBillingData = new Date();
            }
        });
        });
    };
}]);
Run Code Online (Sandbox Code Playgroud)

Kos*_*kin 6

$scope.$watch('result', function () {
        $scope.tableParams.settings().$scope = $scope; <<--nested scope is replacing!!
        $scope.tableParams.reload(); 
    });
Run Code Online (Sandbox Code Playgroud)

ngTable指令创建嵌套范围由inculde的指令工作的所有设置,让你shoudn't与ReportsController的范围取代它
,你需要删除此行 $scope.tableParams.settings().$scope = $scope;
你需要指定所有参数ngTable,请看看这里在ngTables,运行$ scope.tableParams.reload()第三次导致TypeError:无法设置属性'$ data'为null

UPDATE 和也,最好采取ngTableParams intialization,为了防止错误和不必要的重新初始化findReportData功能之外$监视结果

更新2 我设置了普拉克,尽量使其看上去更像原来的问题,和模拟的HTTPService,但是这不是事,所有的数据访问必须在脑子里想getDataFUNC在ngTableParams和设置所有过滤器时,你需要调用(或者只是使用$watch在普拉克等),你必须调用$scope.tableParams.reload();,您可以尝试输入到输入上面的一些数字和数据将被更新没有问题.为清楚起见,删除了与排序,分页和过滤相关的所有代码,getData如下所示:

 getData: function($defer, params) {
        var selectedPeriod = $scope.reportBillingPeriod ; //selectedMonth;
         if( selectedPeriod === null )
    selectedPeriod = '0';
    $scope.customers_report = Reports.get({   //Reports.customer_report({
    monthReport: selectedPeriod
    }, function(result ){
    var data = result.customers;
     $scope.tableParams.total(data.length);


    var orderedData = params.sorting() ? $filter('orderBy')(data, params.orderBy()) : data;
        $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));

    });      

        }
Run Code Online (Sandbox Code Playgroud)

并观察过滤器更改,如下所示:

 $scope.reportBillingPeriod = 0;
    $scope.$watch('reportBillingPeriod', function(newValue, oldValue) {
        $scope.tableParams.reload(); 
     });
Run Code Online (Sandbox Code Playgroud)

更新3
除去调用的重复getDataFUNC,编辑在这里 的问题是在铺设$watchreportBillingPeriod变量,以防止我们要检查如何值被改变,喜欢这里的第二数据上传,所以

$scope.reportBillingPeriod = defaultValue;
    $scope.$watch('reportBillingPeriod', function(newValue, oldValue) {
        //this how we prevent second call
        if (newValue!=oldValue)
        $scope.tableParams.reload(); 
     });
Run Code Online (Sandbox Code Playgroud)