【问题标题】:Angular Filter ng-repeat角度过滤器 ng-repeat
【发布时间】:2014-12-23 08:50:12
【问题描述】:

看起来很简单,但我无法让 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}}
 }

【问题讨论】:

    标签: angularjs filter angularjs-ng-repeat


    【解决方案1】:

    你可以试试这样的

    <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

    【讨论】:

      【解决方案2】:

      通过在 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>

      【讨论】:

        猜你喜欢
        • 2015-09-15
        • 2015-05-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-16
        • 2017-02-21
        • 1970-01-01
        相关资源
        最近更新 更多