2015-03-31 63 views
0

我有一个模型的角模块选择值后结合选项角

$scope.employee = {} 
$scope.countries={} 
$scope..employee.Country_id = 10 
$scope.employee.City_id = 50 

然后我以下列方式结合两个选择元件在

$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; 

    }); 

问题是当页面已加载角度绑定国家,并选择employee.Country_id的值,并且$watch检测到模型更改并触发getCities时,角度会将城市绑定到select元素,但它不会选择emp loyee城市

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> 

我应该怎么做才能解决这个问题?

+0

试着改变你的模型,让员工和他的城市在一个特定的数组中。 – sms 2015-03-31 06:55:33

回答

0

尝试使用ng-option而不是select和option。 你的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>