我正在尝试将默认值设置为离子选择选项,在单选按钮上单击"否"。
<ion-select class="brand" name="selectBrand" [(ngModel)]="selectBrand" required>
<ion-option *ngFor="let brand of Brand;" [value]="brand.val">{{brand.name}}</ion-option>
</ion-select>
<ion-col>
<ion-list radio-group name="checkBox" [(ngModel)]="autoManufacturers">
<ion-item>
<ion-label>Yes</ion-label>
<ion-radio [value]="true"></ion-radio>
</ion-item>
<ion-item>
<ion-label>No</ion-label>
<ion-radio (ionSelect)="radioChecked()" [value]="false"></ion-radio>
</ion-item>
</ion-list>
因此,当我在单选按钮中单击"否"时,我需要放置一个默认数组 [1]。
Brand = [
{
"name": "Avin",
"val" : "Avin"
},
{
"name": "Amul",
"val" : "Amul"
},
{
"name": "Arokya",
"val" : "Arokya"
},
{
"name": "Thirumala",
"val" : "Thirumala"
}];
radioChecked(){
console.log("hi");
selectBrand = this.Brand[0];
}
当我尝试这样做时,我得到了空值,有人可以帮助我吗
就像@PierreDuc在注释中所说的那样,使用页面中的属性来保存所选值,以便您可以使用默认值对其进行初始化。
组件代码:
@Component({...})
export class YourPage {
public selectedBrandValue: any;
// ...
constructor(...) {
// Initialize the select with the first value by default
this.selectedBrandValue = this.Brand[0].val;
}
// ...
}
视图:
<ion-select class="brand" [(ngModel)]="selectedBrandValue" required>
<ion-option *ngFor="let brand of Brand;" [value]="brand.val">{{brand.name}}</ion-option>
</ion-select>