I have successfully used ng-repeat to display my data.
Within the ng-repeat result set, one of the data fields is an array of items.
Example: {x:1, y:[2,3,4]}
I want to filter the data based on the values inside the array. Filtering by non-array data is easy, but I am struggling with filtering by values within the array.
Here is the markup I am using:
ng-repeat = "con in cons | filter: usrSingle.username in con.conPlayerList"
(adjusted markup for better example matching
ng-repeat = "con in cons | filter: '3' in con.y"
)
usrSingle is a scope accessible in this controller. I am not receiving any errors, but I'm unable to find relevant examples for this issue.
Additional code has been requested, so here it is below. I should mention that this is a MEAN app, with MongoDB serving the data and a REST API for data calls.
(EDIT) code snippet from the angular module:
// API call for data
app.factory('usrService', function ($resource) {
return $resource('/api/users', {});
});
// factory for storing user data across controllers
app.factory('usrTracker', function () {
var vUsrProfile = {
usrSingle: 'usrSingle'
};
return {
getProperty: function () {
return vUsrProfile;
},
setProperty: function (value) {
vUsrProfile = value;
}
};
});
// angular controller
app.controller('usrPageController', function ($scope, usrTracker, conService, postService) {
var usrSingle = usrTracker.getProperty();
$scope.usrSingle = usrSingle;
$scope.cons = conService.query();
$scope.posts = postService.query();
});
(FINAL EDIT) The answer provided is a valid solution, but I opted for a different approach. I utilized the MongoDB $unwind aggregation to expand my data, as shown below.
Snippet from API file:
// retrieve data
.get(function (req, res) {
// unwind the conPlayerList array field
Con.aggregate([{ $unwind: "$conPlayerList" }], function (err, cons) {
return res.json(cons);
});
})
I then applied a filter based on the specific user I was searching for. Adjusting the Angular HTML markup as follows:
ng-repeat="con in cons | filter:{conPlayerList:usrSingle.username}"
For better example matching, a generic version would be:
ng-repeat="con in cons | filter: {y:'3'} "