Ng-repeat过滤器不起作用

时间:2016-03-14 18:10:22

标签: angularjs angularjs-ng-repeat

我在这里做错了什么?我遵循AngularJS上所述的确切格式

   <div ng-repeat="country in (config.countries | filter:'Canada')">
    {{country.name}}
</div>

我也试过

   <div ng-repeat="country in (config.countries| filter:{name:'Canada'})">
    {{country.name}}
</div>

这些都没有真正过滤我的结果。

感谢您的帮助

更新

  

json结构

"countries":{
      "Azerbaijan":{"name": "Azerbaijan", "code": "AZ","currency" :"AZN"},
      "Bahamas":{"name": "Bahamas", "code": "BS","currency" :"BSD"},
      "Bahrain":{"name": "Bahrain", "code": "BH","currency" :"BHD"},
      "Bangladesh":{"name": "Bangladesh", "code": "BD","currency" :"BDT"},

2 个答案:

答案 0 :(得分:1)

您的过滤器必须与此类似。

&#13;
&#13;
function Main($scope) {
    $scope.countries = {
     "Azerbaijan":{"name": "Azerbaijan", "code": "AZ","currency" :"AZN"},
      "Bahamas":{"name": "Bahamas", "code": "BS","currency" :"BSD"},
      "Bahrain":{"name": "Bahrain", "code": "BH","currency" :"BHD"},
      "Bangladesh":{"name": "Bangladesh", "code": "BD","currency" :"BDT"}
};
  
$scope.myFilter = function(countries,filter) {
  var result = {};
    angular.forEach(countries, function(value,key) {
      if (value.name == filter) {
        result[key] = value;
      }
    });
    return result;
  }
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
  <div ng-controller="Main">
    <ul>
      <li ng-repeat="(key,value) in myFilter(countries,'Azerbaijan')">
        {{value.name}}
      </li>
    </ul>
   </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您能否告诉我们UITableView的结构/数据?我尝试了你的第二个片段 config.countries 它工作正常。