简体   繁体   English

在ng-options中为ng-model设置初始值

[英]Setting initial value for ng-model in ng-options

I've searched Stack Overflow for an answer to my question but I can't seem to find a solution that works. 我已经在Stack Overflow上搜索了我的问题的答案,但似乎找不到有效的解决方案。

I have two select tag that follow this format: 我有两个遵循此格式的选择标签:

select(required class='form-control nf-input' ng-model='Game.loadedGame.teams[0].teamInfo' ng-change='Game.addTeam(Game.loadedGame.teams[0])',
                             ng-options='team.name for team in Game.teams | orderBy:"name" | preventSameMatchTeams:Game.loadedGame.teams[1].teamInfo.id'
                             )

I am trying to initialize the ng-model for this select by passing in Game.loadedGame into this function after the server responds with the correct data: 我试图通过在服务器以正确的数据响应后将Game.loadedGame传递给此函数来初始化此选择的ng模型:

function setLoadedGameTeams(loadedGame, teams) {
        loadedGame.teams.forEach(matchTeam => {
            let index = teams.findIndex(team => team.id === matchTeam.id);
            if (index !== -1) {
                matchTeam.teamInfo = teams[index];  
            }
        });
    }

The select dropdown isn't changing to the correct option after I set the teamInfo property on each team, even though the view is properly updated to show each team's information. 在我为每个团队设置了teamInfo属性后,即使该视图已正确更新以显示每个团队的信息,选择下拉列表也不会更改为正确的选项。

Any help is appreciated! 任何帮助表示赞赏!

The solution I discovered is very simple. 我发现的解决方案非常简单。 I simply had to add a track by to the ng-options! 我只需要向ng-options添加曲目即可!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM