I use ng-repeat to display my data. This works fine.
One of the data fields in my ng-repeat result set is an array of elements.
Example: {x:1, y:[2,3,4]}
I would like to filter data by data in an array. I can easily filter data without an array, but I am having problems when I try to filter by viewing inisde in the array.
Here is my markup
ng-repeat = "con in cons | filter: usrSingle.username in con.conPlayerList"
(edited markup to fit my example is better ng-repeat = "con in cons | filter: '3' in con.y"
)
usrSingle is an area in this controller that I can access. I have no errors, and I can not find examples of this.
Additional code was requested, and it is below. I forgot to mention that this is a MEAN application. I have a MongoDB serving data. I use the REST API for my data calls.
(EDIT) from angular module:
app.factory('usrService', function ($resource) {
return $resource('/api/users', {});
});
app.factory('usrTracker', function () {
var vUsrProfile = {
usrSingle: 'usrSingle'
};
return {
getProperty: function () {
return vUsrProfile;
},
setProperty: function (value) {
vUsrProfile = value;
}
};
});
app.controller('usrPageController', function ($scope, usrTracker, conService, postService) {
var usrSingle = usrTracker.getProperty();
$scope.usrSingle = usrSingle;
$scope.cons = conService.query();
$scope.posts = postService.query();
});
(FINAL EDITING) The answer outlined for this is a good solution. But I went the other way. I used mongoDB $ unwinding aggregation to explode my data as shown below.
code from the API file:
.get(function (req, res) {
Con.aggregate([{ $unwind: "$conPlayerList" }], function (err, cons) {
return res.json(cons);
});
})
Then I filtered out the user I was looking for. Change HTML angular markup to
ng-repeat="con in cons | filter:{conPlayerList:usrSingle.username}"
to better fit my example, deleting my code would be:
ng-repeat="con in cons | filter: {y:'3'} "