过滤ID

时间:2016-10-14 23:48:18

标签: angular

您好我想只显示wishlist数组中包含userid的数据。我做错了什么?

items = { "_id" : ObjectId("58016d54c6622964cc8906be"), "wishlist":[5893278927277749594, 5893278927277749595, 5893278927277749596] "country" : "Ireland" }

管道文件

import {Pipe} from 'angular2/core';


@Pipe({
  name: 'WishListFilter'
})
export class WishListPipe {

  transform(items, args?): any {
        return items.filter(items => items.wishlist == args.userId);
    }


}

组件文件

import { WishListPipe } from './wishlist-pipe';

@Component({
  selector: 'wislist',
  pipes: [ WishListPipe ],
  template: `
    <ul>
      <li *ngFor="let item of (items | WishListFilter:this.userId">{{item}}</li>
    </ul>
  `
})

export class MainSite(){

public userId = '5893278927277749595'

}

1 个答案:

答案 0 :(得分:0)

请勿在模板中使用this

<li *ngFor="let item of (items | WishListFilter :userId)">{{item}}</li>