簡體   English   中英

angularjs過濾器| 不過濾所有對象屬性

[英]angularjs filter | do not filter all object properties

一個簡單的`angularjs'過濾器實現如下:

<!DOCTYPE html>
<html ng-app="app">
<head>
    <title>angularjs</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
    <script src="js/app.js"></script>
</head>
<body>
    <input type="text" name="query" value="" placeholder="query" ng-model="q">
    <ol ng-controller="ItemsController">
        <li ng-repeat="item in items | filter: q">
            {{item.name}}
        </li>
    </ol>
</body>
<script type="text/javascript">
    'use strict';
    (function() {
      var app = angular.module('app', []);

      app.value('items', [
        {name: 'Item 3', position: 3, color: 'red', price: 2.75},
        {name: 'Item 1', position: 1, color: 'red', price: 0.92},
        {name: 'Item 4', position: 4, color: 'blue', price: 3.09},
        {name: 'Item 2', position: 2, color: 'red', price: 1.95}
      ]);

      app.controller('ItemsController', function($scope, items) {
        $scope.items = items;
      });
    })();
</script>
</html>

當我在輸入框中搜索時,它會搜索items數組中對象的所有屬性,包括positionprice即使只重復name屬性。 即如果我搜索2,則返回item 1item 2item 3 但我只想要第2項。如何將搜索鎖定為僅對象的可見屬性?

將您的過濾器更改為:

<li ng-repeat="item in items | filter: {name: q}">
    {{item.name}}
</li>

這是一個plnkr

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM