javascript - how to filter the data by obj's attribute in ng-repeat angularjs -
im beginning student on angularjs
suppose have josn obj
[ { "title": "a", "date": "2015-05-31", "a": "11", "b": 22, }, { "title": "b", "date": "2015-05-11", "a": "33", "b": 44, }, { "title": "c", "date": "2015-04-11", "a": "55", "b": 66, }, { "title": "d", "date": "2015-03-03", "a": "11", "b": 22, } ] ngrepeat
<li ng-repeat="obj in objs">{{obj.date | date:"mm/yyyy"}}</li> we have 2 data in may (05-31 ,05-11)in json obj,and want keep 1 data in month, keep second one.
how write fliter i'm confused
you need create filter , use inside ng-repeat. syntax create filters this:
angular .module('mymodule') .filter('myfilter', function myfilter(service1, service2) { //to ask deps return function(input) { //here real filtering return dosomethingwithinput(input); } }) to apply logic filtering ask for, can create filter this:
angular .module('mymodule') .filter('oncepermonth', oncepermonth); function oncepermonth() { return function (data) { var filtered = [], months = []; data.foreach(function(item){ var month = item.date.substr(0,7); if (~~months.indexof(month)){ months.push(month); filtered.push(item); } }); return filtered; } } with filter, html template this:
<li ng-repeat="obj in vm.objs | oncepermonth"> {{obj.date | date:"mm/yyyy"}} {{obj.title}} </li> you can find working plnkr here: http://plnkr.co/edit/sg0wwpuue0cjwozj2kus?p=preview
Comments
Post a Comment