[英]Ng-repeat to compare and count objects based on value
I need help to "make" NG-REPEAT and whole limitTo aware of food component ng-show logic, how can i achieve that? 我需要帮助来“制作” NG-REPEAT和整个限制要了解食物成分ng-show的逻辑,我该如何实现? In other words, how can i insert that logic to ng-repeat and limiter?
换句话说,我该如何将该逻辑插入ng-repeat和Limiter?
JS side / HTML side? JS端/ HTML端? Any help would be appreciated.
任何帮助,将不胜感激。 Thanks!
谢谢!
Here is what i mean in code; 这就是我在代码中的意思;
<li
class="xx"
ng-repeat="food in foodlist.$models | filter:search:false | limitTo : foodlength as foodResults track by food.id">
<food
data-food="xxx"
ng-show="(food.$att.random && (search.SomeBoolean || OtherBoolean)) || !food.$att.random"
ui-sref="xx({xx: //irrelvant stuff })"
class="xx-xxxx">
</food>
</li>
There are a few ways to acheive this. 有几种方法可以做到这一点。 Your
ng-show
logic should basically move inside the ng-repeat
. 您的
ng-show
逻辑基本上应该在ng-repeat
内部移动。
The easiest way IMO is to add a filtering function to $scope
in your controller, like so: IMO最简单的方法是在控制器的
$scope
中添加过滤功能,如下所示:
... // Controller creation code
function YourController($scope, ...)
{
... // Controller code
$scope.filterFoodItems = function(food)
{
return (food.$att.random && (search.SomeBoolean || OtherBoolean)) || !food.$att.random;
};
}
Next step is to change the filter
value in your ng-repeat
: 下一步是更改
ng-repeat
的filter
值:
<li
class="xx"
ng-repeat="food in foodlist.$models | filter:filterFoodItems | limitTo : foodlength as foodResults track by food.id">
...
</li>
Just for general knowledge, you can also write a custom filter but I don't think it's necessary in your case. 仅出于一般知识,您也可以编写一个自定义过滤器,但我认为您不需要这样做。 Here's how: https://toddmotto.com/everything-about-custom-filters-in-angular-js/
方法如下: https : //toddmotto.com/everything-about-custom-filters-in-angular-js/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.