angular - 绑定动态形成值
问题描述
我有一个“div”,我正在使用“mat-select”。在其中我有多选格式的 countryList 下拉列表。
我的任务是,单击按钮时,我想在“mat-select”中自动填充国家。
<div class="col-sm-5">
<mat-form-field style="width: 372px;">
<mat-label>Select your options</mat-label>
<mat-select formControlName="origin" [(value)]="selected" multiple>
<mat-option *ngFor="let country of countryLists" [value]="country">{{ country }}</mat-option>
</mat-select>
</mat-form-field>
</div>
解决方案
HTML
<div class="col-sm-5">
<mat-form-field style="width: 372px;">
<mat-label>Select your options</mat-label>
<mat-select formControlName="origin" [(value)]="selected" multiple>
<mat-option *ngFor="let country of countryLists" [value]="country">{{ country }}</mat-option>
</mat-select>
</mat-form-field>
<button (click)="populate()" class="btn btn-primary ml-2">Populate</button>
</div>
TS
countryLists: string[] = [];
selected: any;
ngOnInit(): void {
}
populate(): any {
this.countryLists = ['SriLanka', 'India', 'England'];
}
推荐阅读
- javascript - 查找数组中最长的字符串
- c++ - 如何在5之前设置没有值的宽度
- php - 如何同时使用xammp和mysql服务器
- c# - 确定正在使用的语言 aspx 文件
- corda - 运行打开附件ID:5E14F03C543953911C985450FC3B120DB262DCA60961B02D8B8311D039D4FDE2
- python - Kafka Producer 消费 csv
- c# - 当变量名称仅在运行时已知时,如何将变量名称用作 DataGridView 的列标题?
- c# - Android.Views.InflateException: Binary XML file line #1: Error inflating Class BottomNavigationView
- mysql - MySQL 中的 JSON_SEARCH
- java - 如何制作多账户的账户验证程序?