Suppose we have a JSON dataset listing all languages of books:
$scope.data = [{
"title": "Alice in wonderland",
"author": "Lewis Carroll",
"lang": ["en"]
}, {
"title": "Journey to the West",
"author": "Wu Cheng'en",
"lang": ["ch"]
}]
If we only wanted to display English books using a filter in ng-repeat, is it feasible?
For instance:
<div ng-repeat="d in data | filter:d.lang='en'" style="margin-bottom: 2%">
{{d.title}}
</div>
The objective is to achieve this without using any form control like radio buttons. Can this be done?
-EDIT- Thanks @GrizzlyMcBear for pointing me in the right direction! I managed to make it work by using a slightly modified filter function (provided below)
app.filter('MyFilter', function() {
var out = [];
angular.forEach(input, function(i) {
if (i.lang[0] === 'en') {
out.push(i);
}
})
return out;
}
});
And in the HTML:
<div ng-repeat="d in data | MyFilter" style="margin-bottom: 2%">
{{d.title}}
</div>