简体   繁体   English

元素中的角度ng-repeat条件包装项目(ng-repeat组中的过滤器项目)

[英]Angular ng-repeat conditional wrap items in element (filter items in ng-repeat group)

I'm trying to hide occupation name when filtered user is not found in some occupation. 当某些职业中未找到过滤用户时,我试图隐藏职业名称。

JS: JS:

$scope.contacts = [
    {name: 'John', occupation: 'occupation 1'},
    {name: 'George', occupation: 'occupation 2'},
    {name: 'Jeck', occupation: 'occupation 3'},
    {name: 'Paula', occupation: 'occupation 1'},
    {name: 'Scruath', occupation: 'occupation 3'}
];

HTML: HTML:

<input type="text" ng-model="query">
<div ng-repeat="(key, occupation) in contacts | groupBy: 'occupation'">
    <p ng-bind="occupation[0].occupation"></p>
    <div>
        <div ng-repeat="contact in occupation | filter:search">
            <p ng-bind="::contact.fullName"></p>
            <p ng-bind="::contact.email"></p>
        </div>
    </div>
</div>

RESULT: 结果:

Occupation Name: "occupation 1" 职业名称:“职业1”

  • name: "John" 名称:“约翰”
  • name: "Paula" 名称:“宝拉”

Occupation Name: "occupation 2" 职业名称:“职业2”

  • name: "George" 名称:“乔治”

Occupation Name: "occupation 3" 职业名称:“职业3”

  • name: "Jeck" 名称:“ Jeck”
  • name: "Scruath" 名称:“ Scruath”

Any thoughts? 有什么想法吗?

There are two solution to your problem: 您的问题有两种解决方案:

  • Use ng-show on the filtered array. 在过滤后的数组上使用ng-show
  • Use the sorting on the top ng-repeat . 使用顶部ng-repeat上的排序。

Live example on jsfiddle . jsfiddle上的实时示例。

 angular.module('ExampleApp', ['angular.filter']) .controller('ExampleController', function($scope) { $scope.contacts = [{ name: 'John', occupation: 'occupation 1' }, { name: 'George', occupation: 'occupation 2' }, { name: 'Jeck', occupation: 'occupation 3' }, { name: 'Paula', occupation: 'occupation 1' }, { name: 'Scruath', occupation: 'occupation 3' }]; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.8/angular-filter.js"></script> <div ng-app="ExampleApp"> <div ng-controller="ExampleController"> <input type="text" ng-model="query"> <h3> Filter with new array filtered </h3> <div ng-repeat="(key,occupation) in contacts| groupBy: 'occupation'" ng-show="filterOccupation.length"> <p ng-bind="key"></p> <div> <div ng-repeat="contact in filterOccupation = (occupation|filter:query)"> <p ng-bind="::contact.name"></p> </div> </div> </div> <h3> Filter with extra filtered </h3> <div ng-repeat="(key,occupation) in contacts|filter:query | groupBy: 'occupation'"> <p ng-bind="key"></p> <div> <div ng-repeat="contact in occupation|filter:query "> <p ng-bind="::contact.name"></p> </div> </div> </div> </div> </div> 

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

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