角度过滤ng-repeat

时间:2014-12-23 08:50:12

标签: angularjs filter angularjs-ng-repeat

看起来很容易,但我无法 this 工作:

我希望ng-repeat仅在istLand == 1时显示条目。

<body ng-app>
<div ng-controller="Ctrl">
    <div ng-repeat="ort in orte | filter:{istLand : 1}">
        {{ ort.ortsname }}
    </div>
  </div>
</body>


function Ctrl($scope) {
$scope.orte = {"2812197":{"ortsname":"Berlin","istLand":1},
               "2829695":{"ortsname":"Munich","istLand":0}}
 }

2 个答案:

答案 0 :(得分:0)

你可以尝试这样的事情

<div ng-repeat="ort in orte">
    <span ng-if="ort.istLand == 1">{{ ort.ortsname }} </span>
</div>   

如果您遵循以下结构

,您的代码将有效
$scope.orte = [
    {id:"2812197", ortsname:"Berlin", istLand:1},
    {id:"2829695", ortsname:"Munich", istLand:0}
]

这是演示Fiddle

答案 1 :(得分:0)

通过在angularjs中使用自定义过滤器,实现相同的功能。

function Ctrl($scope) {
    $scope.orte = {"2812197":{"ortsname":"Berlin","istLand":1},"2829695":{"ortsname":"Munich","istLand":0},"2829694":{"ortsname":"Delhi","istLand":1},"2829696":{"ortsname":"Beijing","istLand":0},"2829698":{"ortsname":"Sydney","istLand":1}}
}
angular.module('myApp', []).
  filter('istLandCheck', function() {
    return function(orte,istLand) {
        var out=[];
        angular.forEach(orte, function(ort,value){
            if(ort.istLand==1){
                out.push(ort);
            }
        });
        return out;
    }
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<body ng-app="myApp">
    <div ng-controller="Ctrl">
        <div ng-repeat="ort in orte|istLandCheck">
           {{ ort.ortsname }}
        </div>    
    </div>
</body>