小编mar*_*ibi的帖子

打字稿对象集

我是 angular2 和打字稿的新手。我在创建像 Set<> 这样的独特集合时遇到了问题。我想避免集合中的重复对象,为此,尝试使用如下代码设置的数据类型:

private cardItems = new Set<MyBean>([]);
Run Code Online (Sandbox Code Playgroud)

MyBean 是一个对象。

export class MyBean  {

  id:integer
  ownerId:integer
  ownerName:string
  img: string;

constructor() {

}
public equals(obj: MyBean) {
    console.log(obj.id);
    if (this.id == obj.id) {
        console.log(obj.id);
        return true;
    }
    if (obj == null)
        return false;

    return true;
}

public hashCode(obj: MyBean) {
    return obj.id
}
Run Code Online (Sandbox Code Playgroud)

}

但 equals 和 hashCode 不会以这种方式运行。我在集合中有重复的对象。

实现 Set 的解决方案是什么?

非常感谢

collections set typescript angular

6
推荐指数
2
解决办法
1万
查看次数

如何强制自定义过滤管道在angular2中执行

我在angular2中有自定义过滤的问题.

这是我的情景:

我的页面包含几个自定义组件.其中一个负责显示页面左侧的数据:(componentA)

  <md-list-item *ngFor="let item of items | filter :  filter | sort:   sort; let i = index"  " >
       <template [render]="itemTemplateRef" [context.item]="item" 
       [context.index]="index"></template>
   </md-list-item>
Run Code Online (Sandbox Code Playgroud)

而且,过滤是通过自定义过滤完成的:

    import {Pipe, PipeTransform} from '@angular/core';



@Pipe({ name: 'filter' })

export class FilterPipe implements PipeTransform {
    transform(values: any[], filter: any): any {
        if (typeof filter !== "function") return values;
          return values.filter((elem) => {
             return filter(elem);
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

在主页面中,我将一个函数作为输入属性发送到组件A中:

   public filter = (element: MyBean) => {
             return (element.email !== undefined);

    }
Run Code Online (Sandbox Code Playgroud)

在加载页面时,它执行没有任何问题.但我想通过单击复选框多次更改过滤条件.我知道这个事实,如果输入值改变,则执行纯滤波.但是我不知道,我的场景中哪个输入值应该改为强制过滤?

当我在复选框的Onchange事件中添加以下函数时,没有任何内容被过滤.(项目绑定到我的组件显示数据)

    createFilterCondition …
Run Code Online (Sandbox Code Playgroud)

pipe filter angular

2
推荐指数
1
解决办法
1868
查看次数

标签 统计

angular ×2

collections ×1

filter ×1

pipe ×1

set ×1

typescript ×1