1

我正在尝试显示联赛中球队名称的列表,然后选择一个。选择后,它应该将索引league.teams[index]放入homeTeam.teamId。这是HTML:

<select
    ng-model="homeTeam.teamId"
    ng-options="team.name for team in league.teams">
</select>

这里的范围:

$scope.league = {
    teams: [
        { name: "Team#1" },
        { name: "Team#2" }
    ]
};

但它将整个对象添加到homeTeam.teamId中,如下所示:

homeTeam: {
    "teamId": { "name":"Team#1" }
}

我怎样才能让它只输入索引,即:

homeTeam: {
    "teamId": 0
}
4

1 回答 1

5

如果你想要一个 id,那么团队应该有一个:

ng-options="team.id as team.name for team in league.teams"

选择:

ng-options="league.teams.indexOf(team) as team.name for team in league.teams"

indexOf()IE < 9 不支持

于 2014-04-15T12:27:03.183 回答