【问题标题】:Angular JS filters working unexpectedly?Angular JS 过滤器意外工作?
【发布时间】:2015-08-17 03:42:15
【问题描述】:

我正在尝试使用 Angularjs 过滤器过滤 json 数据列表 (https://api.myjson.com/bins/5a2aw)。当我只使用过滤器时,不会发生精确匹配,并且 1 显示 id = 1、10、11、12 的结果。将 true 与过滤器一起使用非常奇怪,所有 id 都会出现一个 id 为 7 的结果。

 <html>
    <script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
    <body>
    <div ng-app="myApp" ng-controller="myCtrl">
    <ul>
    <li ng-repeat="x in names |  filter : true : {id: 7}">
    {{x.Name+ " " + x.Quote}}
    </li>
    </ul>
    </div>
</body>
    <script>
    var app = angular.module('myApp',[])
    app.controller('myCtrl',function($scope, $http){
        $http.get("https://api.myjson.com/bins/5a2aw").success(function(response) {$scope.names=response.records;});
    });
    </script>
</html>

【问题讨论】:

    标签: javascript angularjs search


    【解决方案1】:

    您应该将true(精确检查)放在filter 标准之后,而不是放在过滤器的第一个位置。

    标记

    <li ng-repeat="x in names |  filter : {id: 7}: true ">
        {{x.Name+ " " + x.Quote}}
    </li>
    

    语法

    {{ filter_expression | filter : expression : comparator}}
    

    【讨论】:

      【解决方案2】:

      检查这个plunker

      代码:

      ng-repeat="x in names.records |  filter : {id: 1} : true"
      

      【讨论】:

      • 你的答案有什么不同..和我的一样:p
      • 你在这里打败了我:p
      • 相差 5 分钟 :p
      猜你喜欢
      • 2015-04-10
      • 1970-01-01
      • 2015-01-12
      • 2017-04-28
      • 1970-01-01
      • 2013-11-08
      • 1970-01-01
      • 2014-05-05
      • 1970-01-01
      相关资源
      最近更新 更多