Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在“角度”中绑定选项后,从下拉列表中选择值_Javascript_Jquery_Angularjs - Fatal编程技术网

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>

尝试更改您的模型,使员工及其所在城市处于特定阵列中。