AngularJS基于子数组过滤对象的ng-repeat列表

时间:2019-01-17 07:56:34

标签: angularjs

我正在学习Angularjs, 我正在尝试获取具有特定会员ID的国家/地区: 如果该国家/地区的memberId为“ 1”,则应显示。

<ul ng-repeat="country in countries | filter:memberId = 1">
   <li>
      {{country.Name}}
   </li>
</ul>

这是我的列表

        $scope.countries = [
            {
              "Name" : "UK",
              "memberId" : ["3"]
            },
            {
              "Name" : "USA",
              "memberId" : ["2"]          
            },
            {
              "Name" : "Mexico",
              "memberId" : ["1", "4"]                 
            },
            {
              "Name" : "Germany",
              "memberId" : ["1", "3"]                 
            },
            {
              "Name" : "Canada",
              "memberId" : ["1", "4"]                 
            }

        ]

是否可以仅显示memberId为“ 1”的国家/地区?

1 个答案:

答案 0 :(得分:0)

您的代码非常好,以下是您的代码,我刚刚添加了input来显示动态过滤器

angular.module('myApp', []).controller('someCtrl', function($scope) {
    $scope.countries = [
            {
              "Name" : "UK",
              "memberId" : ["3"]
            },
            {
              "Name" : "USA",
              "memberId" : ["2"]          
            },
            {
              "Name" : "Mexico",
              "memberId" : ["1", "4"]                 
            },
            {
              "Name" : "Germany",
              "memberId" : ["1", "3"]                 
            },
            {
              "Name" : "Canada",
              "memberId" : ["1", "4"]                 
            }

        ]
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.9/angular.min.js"></script>
<div ng-app="myApp" ng-controller="someCtrl">
<input type="text" ng-model="memberId"/>
<ul ng-repeat="country in countries | filter:memberId">
   <li>
      {{country.Name}}
   </li>
</ul>

</div>