Angularjs 为什么要下拉选择最后一个值
在这里,我尝试将数据加载到dropdownList中,但为什么默认情况下它会从列表中选择最后一个值 HtmlAngularjs 为什么要下拉选择最后一个值,angularjs,angular-ngmodel,ng-options,Angularjs,Angular Ngmodel,Ng Options,在这里,我尝试将数据加载到dropdownList中,但为什么默认情况下它会从列表中选择最后一个值 Html <div ng-controller="Part5Controller"> Country : <select ng-model="CountryID" ng-options="I.CountryID as I.CountryName for I in CountryList" ng-change="GetState()"> <opt
<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>
service.js
app.controller('Part5Controller', function ($scope, servicemard) {
getCountrys();
function getCountrys() {
var xx = servicemard.getctrys();
xx.then(function (d) {
$scope.CountryList = d.data;
})
}
})
app.service('servicemard', function ($http) {
this.getctrys = function () {
return $http.get('/Jan/GetCountries')
}
})
Angular的
ng模型
将目标属性的值和小部件的状态保持同步。在您的情况下,这意味着选择了CountryID
等于控制器范围CountryID
的选项。我没有看到控制器的CountryID
的任何定义,因此我认为您应该尽量明确
将空选项设置为
<option value="-1">Select Country</option>
你可能想一举两得。给“提示”选项一个值会使您不得不处理它。也许更好的方法是预先选择并禁用它
<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>
国家:
选择国家
现在,默认情况下会选择提示,但实际上无法为绑定选择提示。根据我的经验,如果绑定中没有选择项,则默认情况下会选择列表中的最后一项。是否可以添加“Jan/GetCountries”响应?
<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>