问题描述:

I am building a simple angular blog app in WordPress using the REST api, and so far have a select dropdown to filter posts by category, but I need to setup a select displaying archives by month.

I am unsure how to create the ng-repeat to loop over the months in which there are posts, and then how to filter the posts by the month selected.

This is how I am doing it for categories, first - within the controller:

$http.get('/wp-json/taxonomies/category/terms').success(function(data){

$scope.categories = data;

});

Then in the view:

<label for="categories">Categories:</label>

<select name="categories" ng-model="filterCat">

<option value="">Select month</option>

<option ng-repeat="category in categories" ng-bind-html="category.name" value="{{category.name}}"></option>

</select>

What I need to do is query the JSON (date) and retrieve only the month (and perhaps the year) for the select dropdown, and then filter those posts.

Thanks so much in advance.

网友答案:

I think it works now! This is based from a Plunkr somebody else posted on filtering by date with angular:

$scope.selectedMonthFilter = function(element) {
    var d = new Date(element.date)
    if (!$scope.selectedMonth) return true;
    return d.getMonth() == $scope.selectedMonth;
}

The rest is identical to the linked Plunkr.

相关阅读:
Top