javascript - Angular ngFor 选择默认显示对象值的选项
问题描述
假设我有一个对象 statusOptions=['Approved', 'Waiting', 'Rejected']
在标记中,我们有:
<div *ngFor="let myObject of objects">
<select (change)="updateStatus($event.target.value)">
<option selected>{{myObject.status}}</option>
<option *ngFor="let status of statusOptions">{{status}}</option>
</select>
<div>
我如何将对象的当前状态显示为默认选定值?截至目前,它将复制并且下拉列表将显示['Approved', 'Waiting', 'Rejected', 'Approved']
对象的当前状态是否为'Approved'
我不需要两种方式绑定,因为我只是获取更改的值并更新我的数据库。
解决方案
通过在选项中设置 [attr.selected] 来解决它。
<div *ngFor="let myObject of objects">
<select (change)="updateStatus($event.target.value)">
<option *ngFor="let status of statusOptions" [attr.selected]="transaction.status === status ? true : null">{{status}}</option>
</select>
推荐阅读
- android - 主动跟踪任务示例在 Mavic Air 中不起作用
- amazon-web-services - Cognito - 导入用户后未发送电子邮件
- elasticsearch - 我应该如何在 Elastic Search 中索引数据库表
- asp.net - 如何为我的客户制作 Visual Studio 2015 网站安装程序?
- php - Laravel 5.6 自定义规则验证
- django - 如何将 django local 与 heroku 中的 postgress 连接起来
- angular - 自定义路线在 Angular 中不起作用
- reactjs - 使用 defaultProps 时打字稿不发出枚举导入
- hyperledger-fabric - 在网络上使用 composer BNA 时将数据直接存储到 CouchDB
- docker - 带有 Bamboo 服务器实例的 Docker 容器关闭了暴露的端口