Html 如何在angular js中设置选定的特定选项。记录来自数据库
我对angularJs有意见。我想要一个下拉列表来选择编辑记录期间的选项。我是angularJs的新手,请帮我做这件事 HTML代码Html 如何在angular js中设置选定的特定选项。记录来自数据库,html,angularjs,Html,Angularjs,我对angularJs有意见。我想要一个下拉列表来选择编辑记录期间的选项。我是angularJs的新手,请帮我做这件事 HTML代码 <select class="form-control" id="txtCities" ng-model='selected' ng-options="c.city_id as c.city_name for c in storesCities"> <option></optio
<select class="form-control" id="txtCities" ng-model='selected'
ng-options="c.city_id as c.city_name for c in storesCities">
<option></option>
</select>
Ajax响应如下所示:
{"data":[{"city_id":410115,"city_name":"Angulo"},{"city_id":410210,"city_name":"india"}
{"city_id":410220,"city_name":"pune"},{"city_id":412830,"city_name":"Delhi"}],"pages":1,"page":"1","result":"OK","message":""}
只是改变了一下
$scope.selected=410210;
请在html的脚本文件中添加这个js文件,你可以在过滤器中使用它
Underscore.js
http://plnkr.co/edit/8B7vVgT7zU3I1VhrO82I?p=info
$scope.selectedCityId = '410210';
$http.get('StoreCitiesData', {
params: {
pageNumber: page
}
}).then(function(response) {
$scope.storesCities = response.data.data;
$scope.selected =
_.filter($scope.storesCities, function (city) {
return city.city_id == $scope.selectedCityId;
});
});
我们可以使用angular.foreach代替filter
$scope.selectedCityId = '410210';
$http.get('StoreCitiesData', {
params: {
pageNumber: page
}
}).then(function(response) {
$scope.storesCities = response.data.data;
$scope.selected =
angular.forEach($scope.storesCities, function(city) {
if(city.city_id == $scope.selectedCityId)
{
return city;
}
});
});
你到底想达到什么目的?嗨,我做过的每个人。。谢谢你的答复。。
$scope.selectedCityId = '410210';
$http.get('StoreCitiesData', {
params: {
pageNumber: page
}
}).then(function(response) {
$scope.storesCities = response.data.data;
$scope.selected =
angular.forEach($scope.storesCities, function(city) {
if(city.city_id == $scope.selectedCityId)
{
return city;
}
});
});