简体   繁体   English

当选择范围更改时更改选择值

[英]Change select value when the scope of the select is changing

I want to make a update on a select value if the scope, who filled the select before, is changing. 如果之前填充选择的作用域正在更改,我想对选择值进行更新。 My application has this structure: 我的应用程序具有以下结构:

mainApp
- storageFactory
- userController
 - userFactory
- teamController
 - teamFactory
- gameController
 - gameFactory

Team Formular (inside teamController)
- select name=player_1 
- select name=player_2
- input name=teamname

Game Formular (inside gameController)
- select name=team_1
- select name=team_2

if created a new team, the scope of the game formular selects are also updated, everything is fine. 如果创建了一个新团队,则游戏制定者选择的范围也会更新,一切都很好。 But if this team will deleted in the teamlist only the scope of the game formular selects will have an update, but i didn't found a nice way to update the select value in this formular, if there is some option was select before the team was deletet. 但是,如果该团队将在团队列表中删除,那么只有游戏制定者选择的范围会进行更新,但是如果在团队之前选择了某些选项,我没有找到一种更新此制定者中选择值的好方法被删除了。

now i run with following code thru each select value in the game formular: 现在,我通过游戏公式中的每个选择值使用以下代码运行:

// check if the first select has a value
var findTeam1=(scopestor.gamescope.game.hasOwnProperty('team_1')) ? 1: 0;
// check if the second select has a value
var findTeam2=(scopestor.gamescope.game.hasOwnProperty('team_2')) ? 1: 0;
// run thru the updatet scope
for( var m in scopestor.gamescope.gameTeamData ){
    // check if the first select has a value
    // and it is also in the scope object
    if(findTeam1===1 && scopestor.gamescope.gameTeamData[m].teamname === scopestor.gamescope.game.team_1.teamname){
        findTeam1=2;
    } 
    // check if the second select has a value
    // and it is also in the scope object
    if(findTeam2===1 && scopestor.gamescope.gameTeamData[m].teamname === scopestor.gamescope.game.team_2.teamname){
        findTeam2=2;
    }
}
// check if the first select has a value 
// and not found in the scope
if(findTeam1===1){
    // set the value empty
    scopestor.gamescope.game.team_1="";
}
// check if the second select has a value 
// and not found in the scope
if(findTeam2===1){
    // set the value empty
    scopestor.gamescope.game.team_2="";
}

i am a beginer with angular and searching for a better way to handle this update of th select values, any answer will be grateful. 我是有经验的初学者,正在寻找更好的方法来处理选择值的更新,任何答案将不胜感激。

this code on github: kickertunier-angular for beginers github上的这段代码: kickertunier-angular

you can have access to the selected value using the ng-model over a select element in the dom for more information look at the documentation , an remember that angularjs uses double binding between the DOM and your model. 您可以在dom中的select元素上使用ng-model来访问所选值,以获取更多信息,请参阅文档 ,请记住,angularjs在DOM和模型之间使用双重绑定。

It could be something like 可能是这样的

$scope.teams = [
      {name:'player 1', team:'one'},
      {name:'player 2', team:'one'},
      {name:'player 3', team:'one'},
      {name:'player 4', team:'two'},
      {name:'player 5', team:'two'}
    ];

in your markup use a ng-repeat / ng-model combination 在您的标记中使用ng-repeat / ng-model组合

 <select ng-model="selected" ng-options="member.name for member in teams">
     <option value="">-- choose name --</option>
 </select>

selected 已选

<div style="border:solid 1px black; height:20px"
    ng-style="{'background-color':selected.name}">
</div>

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

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