不幸的是,您不能$watch 搜索词/过滤器。 Angular dataTables 是使 jQuery dataTables runnable 在 Angular 中没有 DOM 冲突等的指令,但内部仍然像往常一样工作 - 以完全无角度的方式 :)
但是,dataTables 每次执行搜索/过滤时都会广播一个search(.dt) 事件,然后您可以直接从 DOM 中提取搜索的值:
angular.element('body').on('search.dt', function() {
var searchTerm = document.querySelector('.dataTables_filter input').value;
console.log('dataTables search : ' + searchTerm);
})
这当然是最简单的类 jQuery 方法。您可能在页面上有多个数据表,并希望为每个搜索提取更详细的信息,然后您可以在您的 Angular 应用程序中使用:
angular.element('body').on('search.dt', function(e, api) {
if (!$scope.$$phase) {
$scope.$apply(function() {
$scope.searchTerm = api.oPreviousSearch;
$scope.searchTerm.table = e.target.id;
})
}
})
$apply 以便从事件处理程序内部更新$scope。 oPreviousSearch 实际上是当前搜索,所以上面在表单上生成了一个$watch'able searchTerm
{
"bCaseInsensitive": true,
"sSearch": "test",
"bRegex": false,
"bSmart": true,
"_hungarianMap": {
"caseInsensitive": "bCaseInsensitive",
"search": "sSearch",
"regex": "bRegex",
"smart": "bSmart"
},
"table": "tableId"
}
查看演示 -> http://plnkr.co/edit/E5Tr7FrLRIVTtguQHFx9?p=preview