Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 如何从筛选器访问ngRepeat迭代器范围_Angularjs_Angularjs Ng Repeat - Fatal编程技术网

Angularjs 如何从筛选器访问ngRepeat迭代器范围

Angularjs 如何从筛选器访问ngRepeat迭代器范围,angularjs,angularjs-ng-repeat,Angularjs,Angularjs Ng Repeat,我正在写一些有点像电子商店的应用程序。我有一个实体列表和复选框来过滤它们。我想让复选框过滤器成为某种通用的过滤器。我计划在checkbox的属性中编写一个表达式,然后使用ngRepeat中的filter在每个实体的范围内求值。问题是如何从该筛选函数访问实体的范围。例如: 我的数据(JS): 我的筛选复选框(HTML): 当然,每次应用该筛选函数时,我都可以从$rootScope创建新范围,然后将所有项属性传递给该新范围,然后在此范围中计算该表达式。但我认为这会消耗大量内存,因为每次用户更改过滤器

我正在写一些有点像电子商店的应用程序。我有一个实体列表和复选框来过滤它们。我想让复选框过滤器成为某种通用的过滤器。我计划在checkbox的属性中编写一个表达式,然后使用ngRepeat中的filter在每个实体的范围内求值。问题是如何从该筛选函数访问实体的范围。例如:

我的数据(JS):

我的筛选复选框(HTML):

当然,每次应用该筛选函数时,我都可以从$rootScope创建新范围,然后将所有项属性传递给该新范围,然后在此范围中计算该表达式。但我认为这会消耗大量内存,因为每次用户更改过滤器时都会创建大量新的作用域


您知道更好的解决方案吗?

如果没有cusrtom过滤器,下面的内容是什么

Fruits & Apple:
<input type="checkbox" data-ng-model="fil" data-ng-true-value="{{category = 'fruits';name = 'Apple'}}" data-ng-false-value="" />
Fruits:
<input type="checkbox" data-ng-model="fil.category" data-ng-true-value="fruits" data-ng-false-value="" />
Vegetables:
<input type="checkbox" data-ng-model="fil.category" data-ng-true-value="vegetables" data-ng-false-value="" />

<ul>
  <li ng-repeat="item in food | filter:fil">{{ item.name }}</li>
</ul>
水果和苹果:
水果:
蔬菜:
  • {{item.name}

请参见实际操作:

据我所知,
customFilterFunction
接受项目数组,而不是单个项目。请参见此示例是,筛选器应用于集合,而不是项。感谢提醒:)如果一个复选框中的表达式使用了多个条件:“类别==‘水果’&&subcategory==‘柑橘’”,如果我想选中多个复选框来过滤所有水果和蔬菜,该怎么办?请参阅更新:o)我使用名称进行设置,但子类别的工作原理应该类似。(编辑:选择时存在一些错误,但您已经想到了:))可能的解决方案之一,但我更喜欢在每次执行函数时创建新的作用域。或者(正如我已经做过的)将对象哈希传递给过滤器属性,然后将该对象与数组项进行比较:)
<label>
  Fruits:
  <input type="checkbox" data-filter="category == 'fruits'" />
<label/>
<ul>
  <li ng-repeat="item in food | filter:customFilterFunction">
  </li>
</ul>
$scope.customFilterFunction = function(item) {
  // here I want to eval that expression written in checkbox's attribute
  // within the scope of each item in food array.
  // The problem is not how to access that expression,
  // but how to access the scope of item in food array.
}
Fruits & Apple:
<input type="checkbox" data-ng-model="fil" data-ng-true-value="{{category = 'fruits';name = 'Apple'}}" data-ng-false-value="" />
Fruits:
<input type="checkbox" data-ng-model="fil.category" data-ng-true-value="fruits" data-ng-false-value="" />
Vegetables:
<input type="checkbox" data-ng-model="fil.category" data-ng-true-value="vegetables" data-ng-false-value="" />

<ul>
  <li ng-repeat="item in food | filter:fil">{{ item.name }}</li>
</ul>