为什么下拉选择最后一个值 [英] Why dropdown selecting last value
问题描述
在这里,我尝试将数据加载到dropdownList中,但是默认情况下,为什么它会从列表中选择最后一个值.
Here i try to Loading data into dropdownList Its Loading But why By default it select last value from the List.
HTML
<div ng-controller="Part5Controller">
Country : <select ng-model="CountryID" ng-options="I.CountryID as I.CountryName for I in CountryList" ng-change="GetState()">
<option value="">Select Country</option>
</select>
</div>
controller.Js
app.controller('Part5Controller', function ($scope, servicemard) {
getCountrys();
function getCountrys() {
var xx = servicemard.getctrys();
xx.then(function (d) {
$scope.CountryList = d.data;
})
}
})
service.js
app.service('servicemard', function ($http) {
this.getctrys = function () {
return $http.get('/Jan/GetCountries')
}
})
推荐答案
您可能想用一块石头杀死两只鸟.为提示"选项提供一个值使您不得不处理它.也许更好的方法是预先选择并禁用它.
You might want to kill two birds with one stone. Giving the "prompt" option a value makes you then have to deal with it. Probably the better way is to pre select and disable it.
<div ng-controller="Part5Controller">
Country : <select ng-model="CountryID" ng-options="I.CountryID as I.CountryName for I in CountryList" ng-change="GetState()">
<option value="" selected disabled>Select Country</option>
</select>
</div>
现在,默认情况下已选择提示,但实际上无法为绑定选择提示.我的经验是在绑定中使用空值而不进行选择会导致默认情况下选择列表中的最后一项.
Now, the prompt is selected by default but cannot be actually selected for the binding. My experience is with a null value on the binding without a selection results in the last item in the list being selected by default.
这篇关于为什么下拉选择最后一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!