2013-03-13 5 views
3

이상한 문제가 있습니다. 내 앱에서 AngularJS를 사용하고 있는데 태그 문제가 있습니다. 도시와 주를 내 요소로 처리하도록 컨트롤러가 있습니다.AngularJS <select> 문제

function MeuController($scope, $http) { 

$scope.states = []; 
$scope.selectedState = ''; 

$scope.cities = []; 
$scope.selectedCity = ''; 

$http.get('/state/GetStates').success(function(result) { 
    $scope.states = result; 
}); 

$scope.getCities = function() { 
    $http.get('/cities/GetCitiesByState?state=' + $scope.selectedState).success(function(result) { 
     $scope.cities = result; 
    }); 
} 
}); 

이 시점에서 모든 것이 정상이며 이해하기 쉽습니다. 하지만 ...

내 요소를 이런 식으로 만들

:

<select class="span2" name="SelectedState" ng-model="selectedState" 
ng-change="getCities()" ng-options="state.ID as state.Name for state in states"> 
    <option></option> 
</select> 

<select class="span6" name="SelectedCity" ng-model="selectedCity" 
ng-options="city.ID as city.Name for city in cities"> 
    <option></option> 
</select> 

가 ... 내 요소가 작성되지 않습니다.

<select class="span2" name="SelectedState" ng-model="selectedState" 
ng-change="getCities()"> 
    <option ng-repeat="state in states" value="state.ID">{{state.Name}}</option> 
</select> 

<select class="span6" name="SelectedCity" ng-model="selectedCity"> 
    <option ng-repeat="city in cities" value="city.ID">{{city.Name}}</option> 
</select> 

지금 값이 요소로 가득 차 있습니다 :이 방법을 시도 할 경우

. 그러나 "selectedState"범위 변수의 값을 변경하더라도 요소가 올바른 값을 "선택"하지 않습니다.

누구나 아는 이유는 무엇입니까?

정말 고마워요!

+0

상태 배열에 대한 샘플 데이터를 게시 할 수 있습니까? 나는이 coz, 당신이 코드를 변경하지 않고 로컬로 테스트하면 작동하지 않는 옵션을 요구하고있다. 주나 도시 모두 오지 않거나 주만 오지 않는지 여부 – rajkamal

답변

5

그래, 실제로 문제는 ng-modelng-options 사이입니다.

당신이 할 경우 :

<select class="span6" name="SelectedCity" ng-model="selectedCity" 
ng-options="city.ID as city.Name for city in cities"></select> 

그런 다음 $scope.selectedCitycity.ID에 할당 그냥 city (이 fiddle 확인) 없습니다.

당신은 이런 식으로 작성해야 :

<select class="span6" name="SelectedCity" ng-model="selectedCity" 
ng-options="city.Name for city in cities"></select> 

확인이 fiddle의 결과입니다.

문제가 해결 되었습니까?