admin管理员组文章数量:1389894
I'm having a weird problem. I'm using AngularJS in my app and I'm having issues with tag. I have a Controller to have cities and states handled by my elements:
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;
});
}
});
At this point, everything is OK and easy to understand. But...
When I create my elements this way:
<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>
... my elements aren't filled.
If I try this way:
<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>
Now the values are filled into the elements. Although, if I change the value of "selectedState" scope variable, my element doesn't "select" the right value?
Anyone knows why?
Thank you so much!
I'm having a weird problem. I'm using AngularJS in my app and I'm having issues with tag. I have a Controller to have cities and states handled by my elements:
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;
});
}
});
At this point, everything is OK and easy to understand. But...
When I create my elements this way:
<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>
... my elements aren't filled.
If I try this way:
<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>
Now the values are filled into the elements. Although, if I change the value of "selectedState" scope variable, my element doesn't "select" the right value?
Anyone knows why?
Thank you so much!
Share Improve this question edited Mar 13, 2013 at 15:09 Stewie 60.4k20 gold badges148 silver badges113 bronze badges asked Mar 13, 2013 at 14:56 napfernandesnapfernandes 1,3692 gold badges23 silver badges48 bronze badges 1- Can you post a sample data for states array. I'm asking this coz, the option which you said not working is working if i test it locally without any change to your code. Whether both state and city are not ing or only the state is not ing ? – Rajkamal Subramanian Commented Mar 13, 2013 at 20:58
1 Answer
Reset to default 5Ok so actually the problem is between ng-model
and ng-options
.
When you do:
<select class="span6" name="SelectedCity" ng-model="selectedCity"
ng-options="city.ID as city.Name for city in cities"></select>
Then $scope.selectedCity
is assigned to city.ID
and not just city
(check this fiddle).
You should write it like this:
<select class="span6" name="SelectedCity" ng-model="selectedCity"
ng-options="city.Name for city in cities"></select>
Check the result in this fiddle.
Does it solve your issue?
本文标签: javascriptAngularJS ltselectgt issuesStack Overflow
版权声明:本文标题:javascript - AngularJS <select> issues - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744572430a2613419.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论