Javascript 在“角度”中绑定选项后,从下拉列表中选择值
我有一个角度模型Javascript 在“角度”中绑定选项后,从下拉列表中选择值,javascript,jquery,angularjs,Javascript,Jquery,Angularjs,我有一个角度模型 $scope.employee = {} $scope.countries={} $scope..employee.Country_id = 10 $scope.employee.City_id = 50 然后,我将按照以下方式绑定两个select元素 $http.get("/api/initdataapi/GetCountry").success(function (data) { $scope.countries = data; }); $scope.$watc
$scope.employee = {}
$scope.countries={}
$scope..employee.Country_id = 10
$scope.employee.City_id = 50
然后,我将按照以下方式绑定两个select元素
$http.get("/api/initdataapi/GetCountry").success(function (data) {
$scope.countries = data;
});
$scope.$watch('employee.Country_id', function (newValue, oldValue) {
var url = "/api/initdataapi/GetByCountry/" + newValue;
$http.get(url).success(function (data) {
$scope.Cities = data;
});
问题是,当页面加载angular绑定国家并选择employee.country_id的值时,$watch detect模型会更改并触发getCities,angular会将城市绑定到select元素中,但不会选择employee city
HTML:
<div class="col-sm-3 premove">
<span>Nationality</span>
<select ng-model="employee.Country_id " >
<option ng-repeat="county in countries" value="{{county.Id}}">{{county.Name_En}}</option>
</select>
</div>
<div class="col-sm-4 premove">
<span>Place of birth -{{employee.City_Id}}</span>
<select ng-model="employee.City_Id">
<option ng-repeat="birthCity in Cities" value="{{birthCity.City_Id}}">{{birthCity.City_Ename}}</option>
</select>
</div>
我应该怎么做才能解决这个问题?尝试使用ng选项而不是select and选项。 你的html应该是
<div class="col-sm-3 premove">
<span>Nationality</span>
<select ng-options="country.Id as country.Name_En for country in countries" ng-model="employee.Country_id"></select>
</div>
<div class="col-sm-4 premove">
<span>Place of birth -{{employee.City_Id}}</span>
<select ng-options="birthCity.City_Id as birthCity.City_Ename for birthCity in Cities" ng-model="employee.City_Id"></select>
</div>
尝试更改您的模型,使员工及其所在城市处于特定阵列中。