html - 以角度编辑时未填充下拉菜单和单选按钮表单数组值
问题描述
在我的表单下拉菜单中,编辑时未填充单选按钮值。我尝试过使用 FormArray。但仍然没有在下拉菜单和单选按钮中获得值。
HTML
<form [formGroup]="staffRegistrationForm">
<mat-label>Role</mat-label>
<mat-select formControlName="roleCode" required>
<mat-option *ngFor="let item of roleList" [value]="item.roleCode">
<span> {{item.description}} </span>
</mat-option>
</mat-select>
<mat-label>STD/ City Code</mat-label>
<mat-select formControlName="homeTelephoneCityCode" required>
<mat-option *ngFor="let phoneExt of phoneExtRefList"
[value]="phoneExt.value">
<span>{{phoneExt.name}} </span>
</mat-option>
</mat-select>
<div class="radio-group">
<mat-label>Staff ID * </mat-label>
<mat-radio-group formControlName="staffId">
<mat-radio-button [value]="id" *ngFor="let id of staffIds">
<span> {{id}} </span>
</mat-radio-button>
</mat-radio-group>
</div>
</form>
我的组件是
export class StaffRegistrationComponent implements OnInit {
staffRegistrationObj: StaffRegistrationObj = new StaffRegistrationObj();
roleList: Array<Role>;
phoneExtRefList = PHONE_EXT_LIST;
staffRegistrationForm: FormGroup;
submitted = false;
staffIds = [];
edit = false;
constructor(
private formBuilder: FormBuilder,
private staffService: StaffService,
) {
this.staffRegistrationForm = this.formBuilder.group({
staffId: [null, [Validators.required]],
role: this.formBuilder.group({[]}),
homeTelephoneCityCode: [null, [Validators.required]],
});
this.staffIds = ['Email', 'Mobile'];
const id = this.activatedRoute.snapshot.paramMap.get('id');
if (id !== null) {
this.getStaffDetails(id);}}}
onSave() {
private getStaffDetails(id: string) {
this.staffService.getStaffDetails(id).subscribe(
(staff: StaffRegistrationObj) => this.editStaffDetails(staff));}
private editStaffDetails(staff: StaffRegistrationObj) {
this.staffRegistrationForm.patchValue({
staffId: staff.staffId,
role: staff.role,
homeTelephoneCityCode: staff.homeTelephoneCityCode,});}
请帮助我如何填充该值。
解决方案
推荐阅读
- c# - 如何使用 ILogger
来自 Azure Functions V2 函数? - javascript - 如何检查布尔值是否作为字符串传递?
- c# - 当只有一列不同时,选择不同的行 Linq
- unicode - 日文字符不可读
- python - 如何使用 Python 库(例如 Paramiko)与 Telnet 和 SSH 进行链式连接
- c# - 当在 asp 中调用下拉列表 OnSelectedIndexChanged 时,单选按钮 OnCheckedChanged 失去其值
- javascript - HtmlUnit WebClient.getPage 不处理 javascript
- compiler-construction - 首先计算并遵循此语法的集合
- azure-devops - 源提供者 - 列出分支 - 什么是 providerName?
- matlab - 如何找到给定维度中 3D 矩阵的最小值?