选择下拉列表中选项的两种标签



使用angularjs,我想更改用作标签的内容。我有一些对象是公司或人员,并使用不同的值作为标签显示。因此,对于下面的例子,我的下拉选项应该是:Aida Whitburg,Jones Investments,Edison Yuen,如果是个人,则使用名称作为值,如果条目是公司,则使用companyName。

{ id: 1,
name: "Aida Whitburg",
type: "person"
},
{ id: 2,
companyName: "Jones Investments",
type: "company"},
{ id: 3,
name: "Edison Yuen",
type: "person"
}
<select id="entityDropdown" 
ng-options="entity as entity.name for entity in ctrl.entities" 
ng-model="ctrl.model.markedEntity" class="form-dropdown" 
ng-change="ctrl.onChangeModel()">
</select>

创建一个带有合并字段的新对象并使用它,类似于

let people = [
{
"id": 1,
"name": "Aida Whitburg",
"type": "person"
},
{
"id": 2,
"companyName": "Jones Investments",
"type": "company"
},
{
"id": 3,
"name": "Edison Yuen",
"type": "person"
}
].map(p => {
p.label = p.name || p.companyName
return p;
});
console.log(people)

最新更新