简体   繁体   English

在自定义过滤器中调用$ scope(angular)

[英]Calling $scope in custom filter (angular)

I'm using this angular filter to filter out objects in an array whose localteam_id properties match a value that is held in $scope.whichMyteam . 我正在使用此角度过滤器来过滤数组中的对象,其localteam_id属性与$scope.whichMyteam保存的值匹配。 This works fine. 这很好用。

VIEW 视图

<div ng-repeat="fixture in getFixtures | filter: {localteam_id: whichMyteam} ">

I want to extend the filter however, to include a section criterion: so it will, in effect, be: 我想扩展过滤器,包括一个部分标准:所以它实际上是:

<div ng-repeat="fixture in getFixtures | filter: {localteam_id: whichMyteam && visitorteam_id: whichMyteam} ">

...but a) this doesn't work, and b) even if it did, it's getting a little cumbersome and seems that it would justify making a custom filter. ...但是a)这不起作用,并且b)即使它确实如此,它也会变得有点麻烦,并且似乎有理由制作自定义滤镜。

So, I tried to make one. 所以,我试着制作一个。 The problem I ran into is I need to reference the $scope.whichMyteam value in the filter, but it seems the filter module can't accept/understand $scope . 我遇到的问题是我需要在过滤器中引用$scope.whichMyteam值,但似乎过滤器模块无法接受/理解$scope This is crucial for the filter to work in my instance, so I'm not sure how to resolve this. 这对于我的实例中的过滤器至关重要,所以我不知道如何解决这个问题。

My filter so far looks like 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 is a simplified version which only attempts to match one property (just did this to test it and reduce the number of moving parts). 以上是一个简化版本,它只试图匹配一个属性(只是这样做来测试它并减少移动部件的数量)。 When I call it in the view though... 当我在视图中调用它时...

<div ng-repeat="fixture in getFixtures | myFixtures">

...it doesn't work. ......它不起作用。 Console logs '$scope is not defined'. 控制台日志'$ scope未定义'。


UPDATE: I 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 is logging a syntax error (I think...) 控制台正在记录语法错误(我认为...)

angular.js:13236 Error: [$parse:syntax]

How about having your filter function return a 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 the filter function passing in your 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> 

You have 2 problems here: 1. How to pass a param, html: 你有两个问题:1。如何传递一个参数,html:

<div ng-repeat="fixture in getFixtures | myFixtures:whichMyteam">

js: JS:

    myFilters.filter('myFixtures', function() {

      return function(input, whichMyteam) {
...
  1. Do not place return statement inside forEach -- it does not make what you think;) 不要将return语句放在forEach中 - 它不会使你想到的;)

So finally your filter is: 所以最后你的过滤器是:

myFilters.filter('myFixtures', function() {

 return function(input, whichMyteam) {

    output = [];

    angular.forEach(input, function(o) {
      if (o.localteam_id === whichMyteam) {
        output.push(o);
      }
    })

    return output;
  }
});

http://plnkr.co/edit/0XFfI8hIdCaJ19gJRYr2?p=preview http://plnkr.co/edit/0XFfI8hIdCaJ19gJRYr2?p=preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM