为了使用多个深层属性进行过滤,我们需要创建自定义过滤器。我的意思是,我们需要创建自己的函数来过滤对象中的数据并返回所需的对象(过滤后的对象)。
例如,我需要从下面的对象中过滤数据 -
[
{
"document":{
"documentid":"1",
"documenttitle":"test 1",
"documentdescription":"abcdef"
}
},
{
"document":{
"documentid":"2",
"documenttitle":"dfjhkjhf",
"documentdescription":"dfhjshfjdhsj"
}
}
]
在HTML中,我们使用ng-repeat来展示文档列表 -
<div>
//search input textbox
<input ng-model="searchDocument" placeholder="Search">
</div>
<div ng-repeat="document in documentList | filter: filteredDocument">
//our html code
</div>
在控制器中,我们编写过滤函数来使用对象的两个属性“documenttitle”和“documentdescription”返回筛选后的对象,代码示例如下 -
function filterDocuments(document)
{
if($scope.searchDocument)
{
if(document.documentTitle.toLowerCase().indexOf($scope.searchDocument.toLowerCase()) !== -1 || document.document.shortDescription.toLowerCase().indexOf($scope.searchDocument.toLowerCase()) !== -1)
{
return document
}
}else {
return document;
}
}
$scope.searchDocument是作用域变量,绑定到搜索文本框(HTML输入标签),用户可以在其中输入要搜索的文本。