angular - 在下拉菜单中以角度显示 API 响应
问题描述
{{data.role}}
<mat-form-field>
<mat-label>Role</mat-label>
<mat-select >
<mat-option>selected</mat-option>
<mat-option *ngFor="let role of roles" [value] = "role">
{{role.viewValue}}
</mat-option>
</mat-select>
</mat-form-field>
{{data.role}}
我提到的是来自 API 的响应。如何在下拉菜单中显示?
解决方案
将您的响应存储在 .ts 文件中的变量中。
rolesdata = []; // define variable
this.rolesdata = response.data.roles; // store response in variable
rolesdata = [{value: 'ADMIN', viewValue: 'Admin'},
{value: 'STAFF', viewValue: 'Staff'}]; // for e.g. our variable have this data
<mat-form-field>
<mat-label>Role</mat-label>
<mat-select >
<mat-option>selected</mat-option>
<mat-option *ngFor="let role of rolesdata " [value] = "{{role.value}}">
{{ role.viewValue }}
</mat-option>
</mat-select>
</mat-form-field>
推荐阅读
- scala - 无法下载流的播放迁移的依赖项(Scala/sbt)
- algorithm - 有向无环图的拓扑排序
- python - 带有嵌套数据的 JSONField 上的 Django Queryset,其中字典键的键名中有连字符
- reactjs - React Type 脚本 Ant 设计步骤设置目标
- android - 按钮背景未加载 - Android Studio
- mysql - mysql配置文件'/opt/bitnami/mysql/conf/my.cnf'不可写
- linux - 从文件中 grep 字符串并读取具有完整变量名的第一个匹配字符串
- c# - 如何从后面的代码调用视图单元内的标签
- c# - 如何读取 JSON 并返回项目的值?
- javascript - 等待多个承诺并从 Firebase 获取一些值