javascript - 如何使垫选择列表自动选择一个选项
问题描述
该列表绑定到 .ts 代码:这是 html :
<mat-label>Тип на апликација:</mat-label>
<mat-select [(value)]="AppId" required #apptype="ngModel" [(ngModel)]="AppId" name="apptype"
>
<mat-option>---</mat-option>
<mat-option *ngFor="let applicationType of applicationTypes" value="applicationType.ID" >
{{applicationType.Name}}
</mat-option>
</mat-select>
<mat-error *ngIf="apptype && apptype.touched && apptype.errors && inputForm.submitted">
<span *ngIf="apptype.errors['required']">Ова поле е задолжително.</span>
</mat-error>
</mat-form-field>
打开页面时如何自动选择其中一个选项?
解决方案
我会直接编辑你的代码,但你没有提供太多,所以这里有一个非常简单的例子:
这是你的ts
文件
@Component({
selector: 'table-basic-example',
styleUrls: ['table-basic-example.css'],
templateUrl: 'table-basic-example.html',
})
export class TableBasicExample {
people: FormGroup;
peopleArray=[{
name:'name 1',
},{
name:'name 2',
},{
name:'name 3',
}]
constructor(private fb: FormBuilder){}
ngOnInit() {
this.people = this.fb.group({
people: [null, Validators.required]
});
const toSelect = this.peopleArray.find(p => p.name == "name 3");
this.people.get('people').setValue(toSelect);
}
}
这是你的html
文件:
<form [formGroup]="people">
<mat-form-field class="full-width">
<mat-select placeholder="person" formControlName="people">
<mat-option>--</mat-option>
<mat-option *ngFor="let person of patientCategories" [value]="person">
{{person.name}}
</mat-option>
</mat-select>
</mat-form-field>
<p>{{people.get('people').value | json}}</p>
</form>
如您所见,您只需获取people
对象 ( FormGroup instance
) 并将其值设置为您想要的任何值。我不能让它比这更微不足道。
推荐阅读
- google-apps-script - 在从多个选项卡创建单独的电子表格时复制和应用条件格式
- php - 在 JSON 文件中的逗号后自动添加换行符?
- powershell - 如何格式化我的字符串以在我的 PowerShell 脚本中被识别为路径?
- ios - 当泛型具有默认值时,使用泛型创建结构实例
- google-apps-script - 在 Google App Script 中将文件的所有权转让给我单位以外的用户
- android - 从片段过渡到片段时工具栏闪烁
- python - 数据集的最小距离
- python - Python 应用程序和 docker/AWS 设计问题
- mysql - 创建触发器但出现未知列错误。if 语句有问题?
- angular - Angular 材料步进器在 ng 服务期间不会编译