Angular 如何根据选择的角度下拉值自动填充输入字段?
我正在尝试根据下拉列表的选定值自动填充输入字段。以下是代码,它不起作用。请建议Angular 如何根据选择的角度下拉值自动填充输入字段?,angular,angular-material,material-ui,angular-forms,Angular,Angular Material,Material Ui,Angular Forms,我正在尝试根据下拉列表的选定值自动填充输入字段。以下是代码,它不起作用。请建议 <form [formGroup]="employeeForm" (ngSubmit)="OnSubmit()"> <mat-form-field appearance="outline"> <mat-label>Employee ID</mat-lab
<form [formGroup]="employeeForm" (ngSubmit)="OnSubmit()">
<mat-form-field appearance="outline">
<mat-label>Employee ID</mat-label>
<input type="text" placeholder="Employee ID" required formControlName="employeeidControl" matInput [matAutocomplete]="auto1">
<mat-autocomplete #auto1="matAutocomplete">
<mat-option *ngFor="let employee of employees" [value]="employee.id">
{{ employee.id }}
</mat-option>
</mat-autocomplete>
<mat-error>
Employee ID is required
</mat-error>
</mat-form-field>
<mat-form-field appearance="outline">
<mat-label>Employee Name</mat-label>
<input type="text" matInput placeholder="Employee Name" value="{{employeeidControl.name}}" required
formControlName="employeenameControl">
<mat-error>
Employee Name is required
</mat-error>
</mat-form-field>
</form>
员工ID
{{employee.id}
需要员工ID
员工姓名
员工姓名是必填项
如果我理解正确,是否要根据所选ID填写名称字段?
可能在您的mat选项中放置click事件并将名称传递给ts。因此:
<mat-option *ngFor="let employee of employees" [value]="employee.id" (click)="test(employee.name)"> {{ employee.id }} </mat-option>
你应该能够从你的表格中读到它。。但是你还没有显示你的表单组..我更新了formI think employeeForm.get('employeeidControl')。值应该有效吗?我理解。它只写id…但我想写id的相对名称,你把id放在表单中。因此,您可以将整个对象放在其中,然后读取名称值,但我想在输入字段中填充员工名称值。您尝试了上面的代码吗?它使用基于所选ID的员工姓名填充字段“employeenameControl”。因此,如果选择“ID 1”,则该字段将填充“nameemployee 1”(例如)Ok。知道了。会让你知道的。太好了!成功了。非常感谢。
test(employeeName){
this.employeeForm.get('employeenameControl').patchValue(employeeName)
}