我有一个<select>
框,我填充了一些对象。我使用group by
将此示例分组为 Team 。但是,当球员队伍发生变化时,该组不会更新。
这是标记:
<select ng-options="player.name group by player.team for player in players"></select>
<button ng-click="change();">Change team name</button>
控制器:
.controller('MainController', function($scope) {
$scope.players = [{
name: 'Player one',
team: 'Denmark'
},
{
name: 'Player two',
team: 'Denmark'
},
{
name: 'Player three',
team: 'England'
},
{
name: 'Player four',
team: 'England'
},
{
name: 'Player five',
team: 'England'
}
];
$scope.change = function() {
$scope.players[0].team = 'Sweden';
};
Here's一个JSFiddle来证明我的问题。
为什么更改后团队名称不会更新?
答案 0 :(得分:1)
请参阅&#34;复杂模型(对象或集合)&#34;在https://docs.angularjs.org/api/ng/directive/ngOptions
AngularJS只能通过引用跟踪对象,如果它们都没有&#34;跟踪&#34;被给予或者#34;倍数&#34;国旗已经设定。
您有几个选择:
一个简单的方法是添加&#34;跟踪(player.name +&#39; |&#39; + player.team),但这有点hackish。
或者在你的change方法中创建一个新对象: $ scope.players [0] = {name:$ scope.players [0] .name,team:&#34; Sweden&#34; }