我正在尝试使用由不同类型组成的mat-select组件来编辑某些成员的firebase数据。
我不确定节点的结构,但是我这样做:
member:
member1:
name:
types:
typekey1 : Title1
typekey3 : Title3
...
types:
typekey1:
key: typekey1
title: Title1
typekey2:
key: typekey2
title: Title2
typekey3:
key: typekey3
title: Title3
...
Run Code Online (Sandbox Code Playgroud)
所以我不能做以下功能
compareFn(t1: Type, t2: Type): boolean {
return t1 && t2 ? t1.key === t2.key : t1 === t2;
}
Run Code Online (Sandbox Code Playgroud)
使用模板
<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>
Run Code Online (Sandbox Code Playgroud)
我似乎找不到在compareFn函数中连接两种类型并在启动组件时选择选项的方法
使用 Angular 2 和 Angular Material 6.4.7
我需要使用 mat-select 组件设置默认选择的选项。
以下代码按原样工作,并在我不使用 [formControl] 绑定时设置默认值。
下面的例子:
<mat-form-field>
<mat-select [(value)]="currentYear" placeholder="Select A Year" [formControl]="year.get('year')" >
<mat-option *ngFor="let y of yearsArr;" [value]="y">{{ y }}</mat-option>
</mat-select>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)
当前年份 (currentYear) 是一个变量,用于设置当前年份的值。
但是,当我使用表单控件绑定时,它无法在 mat-select 组件上设置默认选项。
下面的例子:
<mat-form-field>
<mat-select [(value)]="currentYear" placeholder="Select A Year" [formControl]="year.get('year')" >
<mat-option *ngFor="let y of yearsArr;" [value]="y">{{ y }}</mat-option>
</mat-select>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)
我搜索了堆栈溢出并遇到了这个可行的解决方案,但是我在浏览器控制台中收到警告,我不确定这是否是实现此目的的正确方法。
下面的例子:
<mat-form-field>
<mat-select [(ngModel)]="currentYear" placeholder="Select A Year" [formControl]="year.get('year')" >
<mat-option *ngFor="let y of yearsArr;" [value]="y">{{ y }}</mat-option>
</mat-select>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)
我得到的警告如下:
看起来您在与 formControl …