嘿,我有點角度過濾器問題。 我從wordpress導入帖子,我真的想通過標籤過濾它們,比如現在只顯示標籤= ENG的帖子,或者只顯示標籤= GER的帖子。 這是我的HTML看起來像如何使用角度過濾器過濾json
<div ng-controller="ThreeMainPosts">
<div ng-repeat="threePost in threePosts | filter : {data.data.posts[0].tags[0].slug = 'ENG'} ">
<div three-post="threePost">
<h1>CONTENT</h1></div>
</div>
</div>
控制器
myModule.controller('ThreeMainPosts', function($scope, $location, WordPressNewsService) {
var postsPerPage = 3;
var orderBy = 0;
WordPressNewsService.getPosts(postsPerPage, orderBy).then(function(data) {
$scope.threePosts = data.data.posts;
});
}); 和JSON
{ id: 312, type: "post", slug: "la-bamba-3", url: "sada2", … }
attachments:[]
author:{ id: 1, slug: "ds", name: "hnews", first_name: "", last_name: "", … }
categories:[{ id: 6, slug: "ludzie", title: "Ludzie", description: "", parent: 0, post_count: 2 }]
tags: [{ id: 32, slug: "en", title: "EN", description: "EN", post_count: 1 }]
url:"http://xxx0.co,/?p=312"
previous_url:"http://hirsch-news.iterative.pl/?p=306"
status:"ok"
我試圖讓在控制器中的過濾器,但我只能做這個單一元素,例如:
if(data.data.posts[0].tags[0].slug == "ENG"){
$scope.threePosts = data.data.posts;
}
任何想法的傢伙? 祝你有美好的一天! :)
以下是Angular $過濾器的文檔。 https://docs.angularjs.org/api/ng/filter/filter這適用於簡單的過濾器,但如果你需要更復雜的東西,你可以使用angular.module(「yourModule」)構造你自己的過濾器。filter )。 – Matt
@Matt,這就是我上面所做的。自定義過濾器。只是把他從控制器上關了出來,並不想讓答案過於複雜。但感謝鏈接將包括作爲參考編輯。 – alphapilgrim
哦,我很快就看到了這個答案。然後,另一個可能是使用Angular提供的過濾器 – Matt