使用Angular js ng-repeat指令过滤json对象

时间:2016-04-20 12:51:17

标签: javascript angularjs json angularjs-directive angularjs-ng-repeat

在我的角度js控制器中,我有一个包含大陆的json数组,并且在大陆内部包含一系列国家

 //CONTROLLER
app.controller('CountryController', function(){

    this.continents = 
    [
         {
                name:'Africa',
                countries:
                [
                    { name: 'Algeria', code:'DZ', operators:'3'},
                    { name: 'Angola', code:'AO', operators:'1'},
                    { name: 'Benin', code:'BJ', operators:'2'},
                    { name: 'Burkina Faso', code:'BF', operators:'1'}
                ],
          },


         {
               name:'AsiaPacific',
                countries:
                [
                    { name: 'Afghanistan', code:'AF', operators:'4'},
                    { name: 'Bahrain', code:'BH', operators:'2'}
                ],
          }
    ];

});

在我看来,我希望显示各大洲安排的标签元素中的国家,即我有标签来保存每个大陆的国家。例如,我有一个非洲选项卡,在此选项卡中,我想显示非洲对象中的国家/地区。 我尝试过使用过滤器'在每个标签中仅显示特定大洲的国家/地区,但它不起作用也不会显示任何内容。在检查元素时,代码似乎被评论。这是我尝试使用Angular-js完成的第一项任务,所以我仍在学习绳索。

这就是我的观点

       <div ng-app="countriessupported" class="container container_with_height">
                    <div ng-controller="CountryController as countryCtrl" id="myTabContent"  class="tab-content hidden-xs">
                             <div class="tab-pane fade active in" id="africa">
                                   <div class="col-md-12 countries_col-12" ng-repeat="continent in countryCtrl.continents.name | filter: 'Africa' ">

                                             <a href="">
                                                <div class="col-md-3 country_container" ng-repeat="country in continent.countries">

                                                  <div class="country_name">
                                                      {{ country.name }}
                                                   </div>
                                                </div>
                                           </a>

                                   </div>
                           </div>
                        </div>
            </div>

所以我们的想法是让这些国家在班级col-md-3重复 我怎么能实现这个目标呢?

2 个答案:

答案 0 :(得分:1)

尝试将第一个转发器更改为此ng-repeat="continent in countryCtrl.continents而不是ng-repeat="continent in countryCtrl.continents.name过滤器sholud无论如何都要工作

答案 1 :(得分:1)

试试这个HTML代码

<div ng-app="countriessupported" class="container container_with_height">
    <div ng-controller="CountryController as countryCtrl" id="myTabContent"  class="tab-content hidden-xs">
        <div class="tab-pane fade active in" id="africa">
            <div class="col-md-12 countries_col-12" ng-repeat="continent in countryCtrl.continents | filter: 'Africa' ">
                <a href="">
                    <div class="col-md-3 country_container" ng-repeat="country in continent.countries">
                        <div class="country_name">{{ country.name }}</div>
                    </div>
                </a>
            </div>
        </div>
    </div>
</div>

让我知道这是否有用