我想在一个 div 中显示所有应用的智能表过滤器。我的想法是编写一个监视 ctrl.tableState().search.predicateObject 属性的指令:
directivesModule.directive('filterBar', function() {
return {
restrict:'E',
require:'^stTable',
template: '<div class="row"><div class="col-xs-12 filters"><span class="filters-header">Filters: </span><span class="tag label label-default" ng-repeat="filter in filters"><span>{{filter.name}}</span><a><i class="remove glyphicon glyphicon-remove-sign glyphicon-white"></i></a> </span> </div> </div>',
scope: true,
link:function(scope, element, attr, ctrl){
scope.$watchCollection(ctrl.tableState().search.predicateObject, function(newVal, oldVal) {
console.log(newVal, oldVal);
});
}
};
});
然而,console.log 只被调用一次,显示未定义两次。
我使用以下代码添加过滤器:
directivesModule.directive('addFilter', function() {
return {
restrict:'A',
require:'^stTable',
scope: {
criterion: '@',
value: '@'
},
link:function(scope, element, attr, ctrl){
element.bind('click', function() {
scope.$apply(function() {
ctrl.search(scope.value, scope.criterion);
console.log(ctrl.tableState().search.predicateObject);
});
});
}
};
});