Ng重复以基于值比较和计数对象

时间:2019-02-18 16:11:24

标签: javascript angularjs angularjs-ng-repeat

我需要帮助来“使” NG-REPEAT和整个极限“了解”。要了解食品成分ng-show逻辑,我该如何实现?换句话说,我该如何将该逻辑插入ng-repeat和limiter中?

JS端/ HTML端?任何帮助,将不胜感激。谢谢!

这就是我在代码中的意思;

<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>

1 个答案:

答案 0 :(得分:1)

有几种方法可以做到这一点。 您的ng-show逻辑基本上应该在ng-repeat内部移动。

IMO最简单的方法是向控制器中的$scope添加过滤功能,如下所示:

... // Controller creation code

function YourController($scope, ...)
{
    ... // Controller code

    $scope.filterFoodItems = function(food)
    {
        return (food.$att.random && (search.SomeBoolean || OtherBoolean)) || !food.$att.random;
    };
}

下一步是更改filter中的ng-repeat值:

<li 
    class="xx" 
    ng-repeat="food in foodlist.$models | filter:filterFoodItems | limitTo : foodlength as foodResults track by food.id">
...
</li>

仅出于常识,您还可以编写自定义过滤器,但我认为您的情况没有必要。 就是这样: https://toddmotto.com/everything-about-custom-filters-in-angular-js/