使用带有链接的 firebase 值的 Angular Material mat-select 组件中的 compareWith 函数 [英] Use compareWith function from Angular Material mat-select component with linked firebase value

查看:20
本文介绍了使用带有链接的 firebase 值的 Angular Material mat-select 组件中的 compareWith 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用由不同类型组成的 mat-select 组件来编辑某些成员的 firebase 数据.

I'm trying to edit some member's firebase data with a mat-select component composed of different types.

我不确定我的节点结构,但我这样做了:

I'm not sure with my structure of nodes but I did this :

member:
    member1:
        name: 
        types:
            typekey1 : Title1
            typekey3 : Title3
            ...

types:
    typekey1:
        key: typekey1
        title: Title1
    typekey2:
        key: typekey2
        title: Title2
    typekey3:
        key: typekey3
        title: Title3   
    ...                 

所以我不能做下面的功能

So I can't make the following function

compareFn(t1: Type, t2: Type): boolean { 
return t1 && t2 ? t1.key === t2.key : t1 === t2;
}

使用模板

<mat-form-field>
<mat-select [compareWith]="compareFn" [(ngModel)]="member.types" multiple>
<mat-option 
    *ngFor="let type of types | async" 
    [value]="type">
    {{type.title}}
</mat-option>

我好像在compareFn函数中找不到连接这两种类型的方法,并且在组件启动时选择了该选项

I can't seem to find the way to connect the two kind of type in the compareFn function and have selected the option when the component is launched

推荐答案

对于具有以下结构的对象:

For an object of the following structure:

listOfObjs = [{ name: 'john', id: '1'}, { name: 'jimmy', id: '2'},...]

像这样定义标记:

<mat-form-field>
  <mat-select
    [compareWith]="compareObjects"
    [(ngModel)]="obj">
       <mat-option  *ngFor="let obj of listOfObjs" [value]="obj">
          {{ obj.name }}
       </mat-option>
    </mat-select>
</mat-form-field>

并像这样定义比较函数:

And define comparison function like this:

compareObjects(o1: any, o2: any) {
  if(o1.name == o2.name && o1.id == o2.id )
  return true;
  else return false
}

这篇关于使用带有链接的 firebase 值的 Angular Material mat-select 组件中的 compareWith 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆