按ID数组

时间:2016-09-09 15:18:28

标签: javascript angularjs arrays ionic-framework

我有一个包含我的数据的json文件

[{"id":"349","title":"event 1"},{"id":"350","title":"event 2"},{"id":"351","title":"event 3"}]

用户可以将事件保存到本地存储并保存在数组中

$storage.myEvents = ["349","350"]

我用;

显示页面上的所有事件
<ion-item class="item-icon-right item-brown" ui-sref="detail({id: event.id})" ng-repeat="event in events track by event.id ">
        <h2 class="positive"><b>{{event.title}}</b></h2> 
</ion-item>

我想仅显示他们在$storage.myEvents中存储的用户事件,但我的过滤器无效

<ion-item class="item-icon-right item-brown" ui-sref="detail({id: event.id})"
  ng-repeat="event in events track by $index | filter: {id: $storage.myEvents }">

如果我查询本地存储空间

,则会出现错误Error: filter:notarray Expected array but received: 0
localStorage["ngStorage-myEvents"]
"["349","350"]"

如果我将过滤器更改为

 filter: {id: JSON.parse($storage.myEvents)}

我仍然得到同样的错误......

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

您可以使用自定义过滤器。

  $scope.myFilter = function (e) {
    var s = $scope.$storage.myEvents;
    if (s.indexOf(e.id) != -1) return true;
    return false;
  }

  <ion-item class="item-icon-right item-brown" ui-sref="detail({id: event.id})" 
       ng-repeat="event in events | filter: myFilter track by $index">

答案 1 :(得分:0)

那是一个转发器

event in events track by event.id

似乎在数组中存在具有相同event.id(mb some with undefined id)的事件。