为什么下拉选择最后一个值 [英] Why dropdown selecting last value
问题描述
在这里,我尝试将数据加载到 dropdownList 它的加载中,但为什么默认情况下它从列表中选择最后一个值.
HTML
国家:<select ng-model="CountryID" ng-options="I.CountryID as I.CountryName for I in CountryList" ng-change="GetState()"><option value="">选择国家</option></选择>
controller.Js
app.controller('Part5Controller', function ($scope, servicemard) {getCountrys();函数 getCountrys() {var xx = servicemard.getctrys();xx.then(功能(d){$scope.CountryList = d.data;})}})
service.js
app.service('servicemard', function ($http) {this.getctrys = 函数 () {返回 $http.get('/Jan/GetCountries')}})
你可能想用一颗石头杀死两只鸟.给提示"选项一个值使您必须处理它.可能更好的方法是预先选择并禁用它.
国家:<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></选择>
现在,提示是默认选中的,但不能实际选中进行绑定.我的经验是绑定上的空值没有选择会导致默认选择列表中的最后一项.
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屋!