我正在使用 ngRepeat 渲染表格行:
<tr ng-repeat="User in ReportModel.report" on-finish-render>
<td><span>{{User.name}}</span></td>
</tr>
我有这个 on-finish-render
指令来发出一个事件,如果监听让我知道表格何时准备好:
app.directive('onFinishRender', function ($timeout) {
return {
restrict: 'A',
link: function (scope, element, attr) {
if (scope.$last === true) {
$timeout(function () {
scope.$emit('ngRepeatFinished');
});
}
}
}
});
这就是在 Controller 中发出 ngRepeatFinished 事件时发生的情况:
$scope.$on('ngRepeatFinished', function(ngRepeatFinishedEvent) {
//set a variable to true which datatables directive is watching
//when that variable is true, initiate datatables
$rootScope.ReportReady = true;
});
然后我有 DataTables 指令来查找报告何时准备好:
scope.$watch(
function() {
return $rootScope.ReportReady
},
function(newValue, oldValue) {
if (newValue) {
var dTable = element.dataTable(scope.options);
$rootScope.ReportVisible = true;
}
});
问题是有时报告中没有任何项目,因此不会发出事件并且数据表不会显示。
有什么建议吗?
最佳答案
使用 ng-show 或 ng-hide 根据 ReportModel.report 中是否有数据来显示或隐藏表。因此只需检查数据是否为空即可显示或隐藏。
关于javascript - ngRepeat 无项目时处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34844329/