简体   繁体   English

如何使用ng-hide和ng-repeat根据条件显示元素

[英]How to show element based on condition using ng-hide and ng-repeat

I am using AngularJS and I have a table of items that I want to be able to display based on their color. 我正在使用AngularJS,并且有一个要根据其颜色显示的项目表。 For example, if I click on the "SHOW GREEN" and "SHOW RED" checkboxes, the table will show only the items that are green or red. 例如,如果我单击“ SHOW GREEN”和“ SHOW RED”复选框,该表将仅显示绿色或红色的项目。

This is the item object: 这是item对象:

{"name":"Fire Truck", "color":"red"}

Here are my checkboxes, which when clicked will evaluate to TRUE or FALSE: 这是我的复选框,单击该复选框将得出TRUE或FALSE:

<select id="item_color" ng-model='color'>
   <option value='green'>SHOW GREEN</option>
   <option value='red'>SHOW RED</option>
   <option value='blue'>SHOW BLUE</option>
</select>

And here is my table: 这是我的桌子:

<tr ng-repeat="item in items" ng-hide='???'>
   <td>{{item.name}} </td>
   <td>{{item.color}} </td>
</tr>

So how can I have my table dynamically show the desired items? 那么,如何让我的表动态显示所需的项目? The ideal solution would also allow me to list 3 seperate tables for all items of each color. 理想的解决方案还允许我为每种颜色的所有项目列出3个单独的表格。 I am little stumped on how to go about this. 我对此一无所知。 Any ideas? 有任何想法吗?

<tr ng-repeat="item in items" ng-hide='item.color !== color'>
   <td>{{item.name}} </td>
   <td>{{item.color}} </td>
</tr>

It's that simple. 就这么简单。 Alternatively, use ng-if to not stamp out the inverse at all: 或者,使用ng-if根本不消除逆:

<tr ng-repeat="item in items" ng-if='item.color === color'>
   <td>{{item.name}} </td>
   <td>{{item.color}} </td>
</tr>

Thank you to Antiga for his solution. 感谢Antiga的解决方案。 I went for something similar using a controller function inside ng-hide. 我在ng-hide中使用控制器功能进行了类似的操作。 This works for 3+ conditions. 这适用于3个以上的条件。

<!-- Checkboxes to toggle T/F which colors to show -->
<input type='checkbox' id='full' ng-model='toggle_red'>RED
<input type='checkbox' id='part' ng-model='toggle_green'>GREEN
<input type='checkbox' id='former' ng-model='toggle_blue'>BLUE

<!-- ng-hide will show item if color_hider() evals to false -->
<tr ng-repeat="item in items" ng-hide='color_hider(item.color)'>
   <td>{{item.name}} </td>
   <td>{{item.color}} </td>
</tr>

<script>
// variables for the toggles
$scope.toggle_red = true;
$scope.toggle_green = true;
$scope.toggle_blue = true; 

// evals to false for each color if both conditions are true
$scope.color_hider(color){
    if(color == 'red' && $scope.toggle_red === true){ 
        return false;
    }else if(color == 'green' && $scope.toggle_green === true){
        return false;
    }else if(color == 'blue' && $scope.toggle_blue === true){
        return false;
    }else{
        return true;
    };
};
</script>

Rather than ng-repeating over the entire collection and then hiding individual elements, it might be better to filter the ng-repeat itself: 与其对整个集合进行ng重复然后隐藏单个元素,不如对ng-repeat本身进行过滤可能会更好:

<tr ng-repeat="item in items | filter:{'color': 'green'}">

will ng-repeat over all items whose color is green. 将对所有颜色为绿色的项目进行ng-repeat。

(For more complex conditions you can use a function as a filter instead: https://docs.angularjs.org/api/ng/filter/filter ) (对于更复杂的条件,您可以使用函数作为过滤器: https : //docs.angularjs.org/api/ng/filter/filter

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

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