【问题标题】:Angular NG-Repeat - Built In Filter Not workingAngular NG-Repeat - 内置过滤器不起作用
【发布时间】:2017-04-20 13:41:58
【问题描述】:

我正在使用 Angular 1.5.5 开发一个应用程序。我正在尝试按 ID 过滤一组 Location 对象。 Locations 数组中有 10 个对象。过滤器返回 2 个对象而不是一个对象:

位置数组:

我的 html 模板如下所示:

                        <div class="col-md-3" ng-repeat="location in offerings.Locations | filter : {'ID':event.LocationID}">
                        {{location.Name}}<br />
                        {{location.Address}}, {{location.City}}, {{location.State}} {{location.Zip}}<br />
                        Loc ID = {{location.ID}} eventLocID = {{event.LocationID}}
                    </div>

它返回 2 个结果,而不是 2 = 2 的过滤器:

【问题讨论】:

  • 我不确定,但它看起来像过滤字符串,而不是数字,你有 12,因为它包含 2
  • 我认为这可能是与 event.LocationID 或其类型相关的一些问题。你能提供一个 plunker/fiddle 重现这个吗?
  • 谢谢 tanmay - 看起来这就是问题所在。下面Leguest的回答解决了这个问题:ng-repeat="locations.Locations | filter : {'ID':event.LocationID} : true"
  • @user1231748 你检查我的答案了吗?

标签: angularjs


【解决方案1】:

您可以尝试编写自己的过滤器函数或使用该构造:

  ng-repeat="location in offerings.Locations | filter : {'ID':event.LocationID} : true"

也可以查看这个话题Filter array of objects by attribute with integer value in ng-repeat

【讨论】:

    【解决方案2】:

    演示

    var myApp = angular.module('myApp',[]);
    
    myApp.controller('MyCtrl', function($scope) {
        $scope.event = {"LocationID" : 2}
        $scope.locations = [
         {
         "Address": "address1",
         "city": "city1",
         "ID": 2,
         "Name": "name1",
         "State": "state1",
         "Zip": 243435
         },
         {
         "Address": "address2",
         "city": "city2",
         "ID": 3,
         "Name": "name2",
         "State": "state2",
         "Zip": 243435
         },
          {
         "Address": "address3",
         "city": "city3",
         "ID": 12,
         "Name": "name3",
         "State": "state3",
         "Zip": 243435
         }    
        ];
    });
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <div ng-app="myApp" ng-controller="MyCtrl">
                        <div class="col-md-3" ng-repeat="location in locations | filter:{'ID' : event.LocationID}:true">
                            {{location.Name}}<br />
                            {{location.Address}}, {{location.City}}, {{location.State}} {{location.Zip}}<br />
                            Loc ID = {{location.ID}} eventLocID = {{event.LocationID}}
                        </div>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-24
      • 1970-01-01
      相关资源
      最近更新 更多