Angular 如何设置mat autocomplete的默认值
我在一个页面上有多个自动完成,可以选择值并保存它们。我需要的是,当我下次加载页面时,对于已经选择并保存的值,默认情况下应显示预先选择的值 下面是代码片段-Angular 如何设置mat autocomplete的默认值,angular,angular-material,mat-autocomplete,Angular,Angular Material,Mat Autocomplete,我在一个页面上有多个自动完成,可以选择值并保存它们。我需要的是,当我下次加载页面时,对于已经选择并保存的值,默认情况下应显示预先选择的值 下面是代码片段- <ng-container matColumnDef="course"> <th mat-header-cell *matHeaderCellDef mat-sort-header> Course Section </th> <td mat-cell *matCellDef="
<ng-container matColumnDef="course">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Course Section </th>
<td mat-cell *matCellDef="let course"> {{course.courseSection}}
<mat-form-field>
<input type="text" id="{{course.courseSection}}" placeholder="Pick one" aria-label="Number" matInput [formControl]="myControl"
[matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete" (optionSelected)="onSelectionChanged(course.courseSection, $event)">
<mat-option *ngFor="let option of filteredOptions | async" [value]="option">
{{option}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</td>
</ng-container>
Html-
<mat-option *ngFor="let option of filteredOptions | async"
[value]="checkAssigned(course.courseSection)===''? option : checkAssigned(course.courseSection)">
但它不起作用。有什么建议可以帮助我实现吗
对于设置值,它应该与matAutoComplete列表中使用的格式类似 假设您正在使用like
myNameList=[{name:“我的名字”,id:1},{name:“我的名字2”,id:2}]
然后,您需要设置该值
this.myformName.controls['myControl'].setValue({name: "My Name", id:1}); //Working Code
如果设置如下,它将设置默认值
this.myformName.controls['myControl'].setValue("My Name"); //Not working code
那就不行了
请勾选此项以供参考您可以使用FormControls的setValue方法在ngOnInit()挂钩上设置以前选择的值(如果有),如何做到这一点,因为此处是动态生成的自动完成列表?
this.myformName.controls['myControl'].setValue("My Name"); //Not working code