如何从列表中搜索模式并创建新列表

时间:2016-02-10 15:33:21

标签: angularjs

我正在尝试搜索来自服务的列表中的模式,并使用找到的项目创建新列表。 这是我的代码。

 itemList = itemService.getList();

itemList包含以下json数据。

   [
      {
      "emailId": "jhonson@test.com",
      "Id": "100",
      "itemCode":       {
         "id": "1",
         "name": "item"
      },      
      "description": "item 100 description",
      "sendingId": "scott@test.com"      
   },
      {
     "emailId": "ketter@test.com",
      "Id": "101",
      "itemCode":       {
         "id": "1",
         "name": "item"
      },      
      "description": "item 101 description",
      "sendingId": "baker@test.com"  
   },
      {
      "emailId": "paul@test.com",
      "Id": "102",
      "itemCode":       {
         "id": "1",
         "name": "item"
      },      
      "description": "item 102 description",
      "sendingId": "wanda@test.com"  
   },
      {
      "emailId": "wendy@test.com",
      "Id": "103",
      "itemCode":       {
         "id": "1",
         "name": "item"
      },      
      "description": "item 103 description",
      "sendingId": "reil@test.com"  
   },
      {
     "emailId": "bee@test.com",
      "Id": "104",
      "itemCode":       {
         "id": "1",
         "name": "item"
      },      
      "description": "item 104 description",
      "sendingId": "dan@test.com"  
   },
      {
      "emailId": "michael@test.com",
      "Id": "105",
      "itemCode":       {
         "id": "1",
         "name": "item"
      },      
      "description": "item 105 description",
      "sendingId": "kris@test.com"  
   }
]

现在我正在尝试从此列表中搜索模式并创建一个新列表。

searcPattern='jhonson' then the filterList should have these values.
   {
      "emailId": "jhonson@test.com",
      "Id": "100",
      "itemCode":       {
         "id": "1",
         "name": "item"
      },      
      "description": "item 100 description",
      "sendingId": "scott@test.com"      
   }

如何在控制器中使用过滤器?

1 个答案:

答案 0 :(得分:1)

你可以这样做:

var filteredList = $filter('filter')(itemList , 'jhonson');

它将根据您提供的数组和表达式返回已过滤的列表。

为了能够使用$ filter,您还需要将其注入控制器:

function myCtrl($scope, $filter)
{
}

更多信息HERE