arrays - Calling $scope in custom filter (angular) -
i'm using angular filter filter out objects in array localteam_id
properties match value held in $scope.whichmyteam
. works fine.
view
<div ng-repeat="fixture in getfixtures | filter: {localteam_id: whichmyteam} ">
i want extend filter however, include section criterion: will, in effect, be:
<div ng-repeat="fixture in getfixtures | filter: {localteam_id: whichmyteam && visitorteam_id: whichmyteam} ">
...but a) doesn't work, , b) if did, it's getting little cumbersome , seems justify making custom filter.
so, tried make one. problem ran need reference $scope.whichmyteam
value in filter, seems filter module can't accept/understand $scope
. crucial filter work in instance, i'm not sure how resolve this.
my filter far looks this:
app.filter('myfixtures', function() { return function(input) { angular.foreach(input, function(o) { output = []; if (o.localteam_id === $scope.whichmyteam) { output.push(o); } return output; }) }; });
the above simplified version attempts match 1 property (just did test , reduce number of moving parts). when call in view though...
<div ng-repeat="fixture in getfixtures | myfixtures">
...it doesn't work. console logs '$scope not defined'.
update: tried this, still not working!
filter
var myfilters = angular.module('myfilters', []) myfilters.filter('myfixtures', function(whichmyteam) { return function(input) { angular.foreach(input, function(o) { output = []; if (o.localteam_id === whichmyteam) { output.push(o); } return output; }) } });
view
<div ng-repeat="fixture in getfixtures | myfixtures(whichmyteam)">
console logging syntax error (i think...)
angular.js:13236 error: [$parse:syntax]
how having filter function return function.
app.filter('myfixtures', function() { return function(input, whichmyteam) { output = []; angular.foreach(input, function(o) { if (o.localteam_id === whichmyteam) { output.push(o); } }) return output; }; });
then call filter function passing in variable
<div ng-repeat='fixture in getfixtures | myfixtures:whichmyteam'>
--- example
angular .module('app', []) .filter('otherfilter', otherfilter) .controller('controller', controller); controller.$inject = ['$scope']; function controller($scope) { $scope.things = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; $scope.modval = 2; console.log('controller'); $scope.myfilter = function(x) { return function(y) { return (y % x) === 0; } }; } function otherfilter() { return function(y, x) { var out = []; angular.foreach(y, function(val) { if ((val % x) === 0) { out.push(val) } }); return out; } }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app='app' ng-controller='controller'> <div ng-repeat='thing in things | filter:myfilter(modval)'> {{ thing }} </div> <br/> <div ng-repeat='thing in things | otherfilter:modval'> {{ thing }} </div> </div>