javascript - 单击时选择列表中的第一项
问题描述
我有一个列表,当我选择最后一项时,我想选择列表的第一项。
有没有办法做到这一点?
请问有办法处理吗?
<ion-select interface="popover" [ngModel]="selecteduser._id" (ngModelChange)="selectUser($event)" (ionChange)="onChange($event)">
<ion-option *ngFor="let user of users" [value]="user._id">
</ion-content>{{ getUserNickname(user }} </ion-option>
<ion-option>Configuration</ion-option>
</ion-select>
所以我想当我点击我的配置时,它会选择我列表的第一项
有我的ts
openConfigPage() {
this.selectedUser = this.users[0];
this.navCtrl.push(ConfigPage)
}
selectUser(userId: string) {
this.selectedUser = this.users.find(b => b._id === userId)
this.onSelect.emit(this.selectedUser)
}
有没有办法用 DOM 做到这一点?
解决方案
模板
Select Last One <select [(ngModel)]="selectedItem" (ngModelChange)="onSelect($event)">
<option *ngFor="let item of items" [value]="item">{{item}}</option>
</select>
<br><br>
You have selected : {{selectedItem}}
零件
export class AppComponent {
selectedItem:string;
items = [
'Purab', 'Pashchim', 'Uttar', 'Dakshin'
];
onSelect(selectedItem) {
let len = this.items && this.items.length;
this.selectedItem = len && selectedItem === this.items[len-1]?
this.items[0]:selectedItem;
}
}
推荐阅读
- jestjs - 使用 Jest 和 Parcel 导入 ES6 时出错
- c# - Data binding attached property to view model fails
- vhdl - VHDL/GHDL Binary 32-bit Write Overflow When High Bit Set
- opengl - New mesa installed but glxinfo show the older one
- node.js - Variable 'newData' is used before being assigned - TypeScript
- javascript - 禁用输入字段不起作用
- laravel-5 - Laravel Eloquent Fill Model with multiple items
- javascript - 使用 topnav 和事件侦听器调整桌面显示以实现响应式站点导航
- php - 前三行(可折叠)显示在容器中,三行后显示在容器外 - MaterializeCSS
- javascript - p5.js Color Array Error Message