过去两天我一直在为此而努力,我找不到解决办法。我正在尝试根据标签过滤一组文章。我使用ng-repeat重复文章并显示来自我的API的数据。但似乎无法使过滤器正确。
以下是文章对象
的示例{"id":"05390344-57f8-4f2a-ada0-0705edacd0ef","type":"Article","title":"Lorem title","topics":[{"Id":"bb2a6222-34b1-4abd-8d19-c07eb7ff4d0c","Name":"Pajamas"},{"Id":"7f752e06-092b-473a-9f4e-b79796c7ab7b","Name":"Undies"},{"Id":"bd533c42-90e9-4dce-9316-e3be6c8fc55c","Name":"Briefs"}],"synopsis":"<p>Lorem Ipsem synposis</p>","date":"August 20, 2014","time":"9:28 AM","source":"Localhost","url":"#"}
我的控制器包含
$scope.filterTopics = {};
function getChecked(obj) {
var checked = [];
for (var key in obj) if (obj[key]) checked.push(key);
return checked;
}
$scope.searchFilter = function (row) {
var topicChecked = getChecked($scope.filterTopics);
if (topicChecked.length == 0)
return true;
else {
if ($scope.filterTopics[row.topics])
return true;
}
};
$scope.$watch('cards', function (cards) {
$scope.count = 0;
angular.forEach(cards, function (card) {
if (card.filterTopics) {
$scope.count += 1;
}
})
});
我创建ng-repeat
的观点<div ng-repeat="article in articles | filter: searchFilter">
然后我有复选框,我可以点击以通过搜索过滤主题ID
ng-model="filterTopics['@topic.Id']
任何帮助都会很棒!!!!
解决方案是添加一个for循环来抓取每个对象
for (var i in row.topics) {
if (topicChecked.indexOf(row.topics[i].Id) != -1) {
return true;
}
}