Angularjs 1.2在select中添加了空选项

时间:2015-12-18 03:35:49

标签: asp.net angularjs

我有一个从数据库中获取其项目的下拉列表。在此下拉列表中选择的默认值是从cookie中检索的。 问题是,
1.有一个空选项标签作为第一个选项,并且,
2.未选择默认值 使用Javascript时完全没有问题。使用<script type="text/javascript"> var computerChoice = Math.random(); if (computerChoice < 0.33) { computerChoice = "rock"; } else if(computerChoice <= 0.66) { computerChoice = "paper"; } else { computerChoice = "scissors"; } for(i=1; i<6; i++) { var counter =0; document.getElementById("box1").onclick= function(){ if(computerChoice == "rock"){ alert("It is a tie, You chose Rock, computer chose Rock, Lame!"); } else if(computerChoice =="paper"){ alert("Sucker, YOU LOST! You chose Rock, COMPUTER OVERLORD chose paper"); } else if(computerChoice=="scissors"){ alert("DANG! You Beat Computer OVERLORD cuz he chose scissors"); counter++; } } document.getElementById("box2").onclick= function(){ if(computerChoice == "paper"){ alert("It is a tie, You chose Paper, computer chose Paper, Lame!"); } else if(computerChoice =="scissors"){ alert("Sucker, YOU LOST! You chose Paper, COMPUTER OVERLORD chose scissors"); } else if(computerChoice=="rock"){ alert("DANG! You Beat Computer OVERLORD cuz he chose rock"); counter++; } } document.getElementById("box3").onclick= function(){ if(computerChoice == "scissors"){ alert("It is a tie, You chose scissors, computer chose scissors, Lame!"); } else if(computerChoice =="rock"){ alert("Sucker, YOU LOST! You chose scissors, COMPUTER OVERLORD chose rock"); } else if(computerChoice=="paper"){ alert("DANG! You Beat Computer OVERLORD cuz he chose paper"); counter++; } } i++; return counter; } var computerWins = 5-counter; if (computerWins > counter) { console.log("COMPUTER OVERLORD WINS, HE IS YOUR MASTER!"); } else { console.log("Hey computer overlord and you can be friends, just dont tell anyone you lost, k"); } </script> 时会发生这种情况。 (我只能使用v1.2.0)
如何解决这个问题。

ASPX:

v1.3.16

Ctrl.js:

v1.2.0

呈现HTML:

<select id="ddlItems" data-ng-model="ItemId">
 <option value="-1">-- All Circles --</option>
 <option data-ng-repeat="item in Items" value="{{item.AreaCode}}" 
  data-ng-selected="item.AreaCode==ItemId">{{item.AreaName}}</option>
</select>

2 个答案:

答案 0 :(得分:1)

尝试使用 ngOptions

喜欢这个

<select id="ddlItems" 
     data-ng-model="ItemId" 
     ng-options="item.AreaCode as item.AreaName for item in Items">
 <option value="-1">-- All Circles --</option>
</select>

答案 1 :(得分:1)

这是因为当您的模型应用已加载时data-ng-model="ItemId"为空。

要有一些初始价值。您可以将默认值放在ng-init

对于Ex: data-ng-init='data-ng-model="--SELECT--";'

或者根据您的数组列表从数据库中获取的项目设置其中一个值。

请记住 init 将在您完成Ajax调用后被触发。