我认为我需要的只是重新渲染表格的方法或在加载客户端后完成该事实的方法。我在指令内部尝试过$scope.$apply()
,但它抱怨 $digest 循环已经发生。而且我还尝试了内置的 ngTableParams,reload()
但它似乎没有做任何事情。在我的指令中,data
从一个空数组开始,但不久之后填充了大约 20 个项目。它只是不会显示那些更新的结果。
<div class="row">
<j-table data="clients" search="search" class="table small-8 columns end">
<td data-title="'Name'" sortable="'last'">
<span>{{client.first || 'no name'}}</span>
<span>{{client.last || ''}}</span>
</td>
<td data-title="'Address'" sortable="'city'">{{client | formatAddress}}</td>
</j-table>
</div>
angular.module('locqusAngular').directive('jTable', function () {
return {
restrict: 'E',
transclude: true,
scope: {
data: '=',
search: '='
},
template:
'<table ng-table="tableParams"> \
<tr ng-repeat="d in $data | filter:search.term"> \
<div ng-transclude></div> \
</tr> \
</table>',
controller: function ($scope, $filter, ngTableParams) {
$scope.tableParams = new ngTableParams({
page: 1,
count: 10,
sorting: {
name: 'asc',
filter: $scope.search
},
filter: {}
}, {
total: $scope.data.length,
getData: function ($defer, params) {
var orderedData = params.sorting() ? $filter('orderBy')($scope.data, params.orderBy()) : $scope.data;
$defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
}
});
$scope.$watch('data.length', function(newx, oldx) {
if (newx != null && newx !== oldx && newx > 0) {
$scope.tableParams.reload();
}
});
},
}
})