我想做一个依赖选择/下拉。
我获得了这个json样本:
[
{
"id": 15695,
"username": "user1",
"address": {
"id": 16794,
"location": "O Treviño de San Pedro"
},
"jobs": [
{
"id": 7562,
"name": "ut"
},
{
"id": 7565,
"name": "temporibus"
},
{
"id": 7603,
"name": "perspiciatis"
},
{
"id": 7622,
"name": "optio"
}
]
}
]
这是角度代码:
<select ng-model="industrialist.user" ng-options="user.id as user.username for user in users"></select>
<select ng-model="industrialist.job" ng-options="job.id as job.name for job in industrialist.user.jobs"></select>
如果我执行此代码,那么依赖选择可以正常工作,但是我无法在industrialist.user中找到正确的密钥。
<select ng-model="industrialist.user" ng-options="user.username for user in users"></select>
我该怎么办?
由于
答案 0 :(得分:1)
我相信你需要做的是保存整个用户而不仅仅是id。如果需要,你可以在以后获取id。
<select ng-model="industrialist.user" ng-options="user.id as user.username for user in users"></select>
<select ng-model="industrialist.job" ng-options="job.id as job.name for job in industrialist.user.jobs"></select>
这是一个工作小提琴。 JSFiddle