javascript - How to filter by an array in AngularJs ng-repeat? -


i using ng-repeat display data. working fine.

one of data fields in ng-repeat result set array of items. example: {x:1, y:[2,3,4]} filter data data in array. can filter non array data, having trouble when try filer looking inisde in array.

here markup ng-repeat = "con in cons | filter: usrsingle.username in con.conplayerlist"

(edited markup match example better ng-repeat = "con in cons | filter: '3' in con.y" )

usrsingle scope in controller can access. don't errors , can't seem find examples of this.

more code requested , here below. forgot mention mean app. have mongodb serving data. use rest api data calls.

(edit) code angular module:

// call api data app.factory('usrservice', function ($resource) { return $resource('/api/users', {}); }); // factory hold user data between 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) answer marked solution. went direction. used mongodb $unwind aggregation explode data, below.

code api file:

// .get(function (req, res) { // unwind on conplayerlist array field con.aggregate([{ $unwind: "$conplayerlist" }], function (err, cons) { return res.json(cons); }); }) 

then filtered on user looking for. changing html angular markup this

ng-repeat="con in cons | filter:{conplayerlist:usrsingle.username}"

to match example better, removing specific code, be:

ng-repeat="con in cons | filter: {y:'3'} "

you can use angular's built in filter logic on array, bear in mind not exact match , match against array entry of 300 if usrsingle set 3, see example in fiddle.

<div ng-repeat = "con in cons | filter:{y:usrsingle} ">...</div>  

to match against array elements can use filter: , predicate function on controller i.e.

controller (fiddle):

app.controller('usrpagecontroller', function ($scope) {   $scope.filterfn = filterfn;   $scope.usrsingle = 3;   $scope.cons = [       {x:0, y:[1,2,3]},       {x:1, y:[2,3,4]},       {x:2, y:[3,4,5]},       {x:3, y:[4,5,6]},       {x:4, y:[5,6,7]}   ];    function filterfn(con){     return con.y.indexof($scope.usrsingle) > -1;   } }); 

view:

<div ng-repeat = "con in cons | filter:filterfn">...</div> 

alternatively can create custom filter can reused elsewhere in app , pass usrsingle in parameter:

filter (fiddle):

app.filter('customfilter', function() {   return function(input, value) {     var result = [];     input.foreach(function(item){         if(item.y.indexof(value) > -1){             result.push(item);         }     });     return result;   }; }); 

view:

<div ng-repeat = "con in cons | customfilter:usrsingle">...</div> 

Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -