Angular Material: mat-select 使用响应式表单时的默认值
Angular Material: mat-select default value when using reactive forms
我使用反应式表单和 angular material mat-select 创建了一个表单,我用它来创建一个名为“core 的对象”。核心对象有 2 个属性:"name" 和 "owners"。 "owners" 属性 是 IUser 对象的数组。
核心对象:
export interface ICore {
id?: number;
name: string;
owners: IUser[];
}
形式:
<form [formGroup]="parentForm">
<mat-form-field class="example-full-width">
<input matInput formControlName="name" placeholder="Name">
</mat-form-field>
<mat-form-field class="example-full-width">
<mat-label>Users</mat-label>
<mat-select formControlName="owners" multiple>
<mat-option *ngFor="let user of users" [value]="user"> {{ user.username }}</mat-option>
</mat-select>
</mat-form-field>
</form>
“users”变量是可以select编辑的所有可用用户的数组。
users: IUser[];
表单效果很好,但现在我想使用相同的表单来编辑核心对象,为此我需要在加载页面时在 "owners" 表单控件中显示核心所有者。
表单创建:
createForm() {
this.coreForm = this.formBuilder.group({
name: ['', [Validators.required]],
owners: ['', [Validators.required]]
});
}
我如何获得核心:
getCore() {
this.coreService.getCore(this.route.snapshot.params['id'])
.subscribe(
res => {
this.core = res;
this.updateForm();
},
err => this.toastr.error('Core has not been received!', 'Error!')
);
}
表单更新(适用于名字属性):
updateForm() {
this.coreForm.patchValue({
name: this.core.name,
owners: this.core.owners
});
}
但是没有添加所有者列表。奇怪的是,如果我用 users 更新表单,它会起作用:
getUsers() {
this.usersService.getUsers()
.subscribe(
res => {
this.users = res;
this.coreForm.patchValue({
owners: res
});
},
err => this.toastr.error('User could not be received!', 'Error!')
);
}
这样所有的用户都添加为默认值。但对于核心所有者来说,这是行不通的。知道我在这里做错了什么吗?
提前致谢!
您需要使用 [compareWith]
指令定义对象被视为选中的条件,如下所示:
<form [formGroup]="parentForm">
<mat-form-field class="example-full-width">
<input matInput formControlName="name" placeholder="Name">
</mat-form-field>
<mat-form-field class="example-full-width">
<mat-label>Users</mat-label>
<mat-select [compareWith]="compareFunction" formControlName="owners" multiple>
<mat-option *ngFor="let user of users" [value]="user"> {{ user.username }}</mat-option>
</mat-select>
</mat-form-field>
</form>
并定义您的比较函数:
compareFunction(o1: any, o2: any) {
return (o1.name == o2.name && o1.id == o2.id);
}
我同意上面的回答我将此发布给拥有 Angular mat-select 并使用反应式表单的用户。
您可以使用 Angular 响应式表单的 patchValue 来获得默认值 selected
在您的组件中的 ngOnInit() 或任何您想要下拉菜单的地方 selected
像下面的代码一样调用 patchValue 函数:
this.yourFrom.patchValue({
CategoryID:1
});
我使用反应式表单和 angular material mat-select 创建了一个表单,我用它来创建一个名为“core 的对象”。核心对象有 2 个属性:"name" 和 "owners"。 "owners" 属性 是 IUser 对象的数组。
核心对象:
export interface ICore {
id?: number;
name: string;
owners: IUser[];
}
形式:
<form [formGroup]="parentForm">
<mat-form-field class="example-full-width">
<input matInput formControlName="name" placeholder="Name">
</mat-form-field>
<mat-form-field class="example-full-width">
<mat-label>Users</mat-label>
<mat-select formControlName="owners" multiple>
<mat-option *ngFor="let user of users" [value]="user"> {{ user.username }}</mat-option>
</mat-select>
</mat-form-field>
</form>
“users”变量是可以select编辑的所有可用用户的数组。
users: IUser[];
表单效果很好,但现在我想使用相同的表单来编辑核心对象,为此我需要在加载页面时在 "owners" 表单控件中显示核心所有者。
表单创建:
createForm() {
this.coreForm = this.formBuilder.group({
name: ['', [Validators.required]],
owners: ['', [Validators.required]]
});
}
我如何获得核心:
getCore() {
this.coreService.getCore(this.route.snapshot.params['id'])
.subscribe(
res => {
this.core = res;
this.updateForm();
},
err => this.toastr.error('Core has not been received!', 'Error!')
);
}
表单更新(适用于名字属性):
updateForm() {
this.coreForm.patchValue({
name: this.core.name,
owners: this.core.owners
});
}
但是没有添加所有者列表。奇怪的是,如果我用 users 更新表单,它会起作用:
getUsers() {
this.usersService.getUsers()
.subscribe(
res => {
this.users = res;
this.coreForm.patchValue({
owners: res
});
},
err => this.toastr.error('User could not be received!', 'Error!')
);
}
这样所有的用户都添加为默认值。但对于核心所有者来说,这是行不通的。知道我在这里做错了什么吗?
提前致谢!
您需要使用 [compareWith]
指令定义对象被视为选中的条件,如下所示:
<form [formGroup]="parentForm">
<mat-form-field class="example-full-width">
<input matInput formControlName="name" placeholder="Name">
</mat-form-field>
<mat-form-field class="example-full-width">
<mat-label>Users</mat-label>
<mat-select [compareWith]="compareFunction" formControlName="owners" multiple>
<mat-option *ngFor="let user of users" [value]="user"> {{ user.username }}</mat-option>
</mat-select>
</mat-form-field>
</form>
并定义您的比较函数:
compareFunction(o1: any, o2: any) {
return (o1.name == o2.name && o1.id == o2.id);
}
我同意上面的回答我将此发布给拥有 Angular mat-select 并使用反应式表单的用户。
您可以使用 Angular 响应式表单的 patchValue 来获得默认值 selected
在您的组件中的 ngOnInit() 或任何您想要下拉菜单的地方 selected 像下面的代码一样调用 patchValue 函数:
this.yourFrom.patchValue({
CategoryID:1
});