[英]Load default option of database into selected option
I have a function that load a simple select like: 我有一个函数可以加载一个简单的选择,如:
function cargarCatalogo() {
apiService.get("../../api/Catalogo/GetCatalogoPadre/" + $scope.Catalogo + "/",
null,
function(res) {
$scope.Catalogos = res.data;
$scope.selected = $scope.Catalogos[0];
inicial
$scope.filtro($scope.selected);
},
errorCatalogo);
}
As you can see I select first option as default with $scope.selected = $scope.Catalogos[0];
如您所见,我使用$scope.selected = $scope.Catalogos[0];
选择第一个选项作为默认选项$scope.selected = $scope.Catalogos[0];
Problem is I don´t want that option as selected I want to be selected value of database because I´m ussing Edit View. 问题是我不希望该选项被选中,因为我使用的是“编辑视图”,所以我想选择数据库的值。
My table is like this: 我的桌子是这样的:
+--------+----------+---------+
| ID | Nombre | PadreID |
+--------+----------+---------+
| 1 | KENWORTH | 0 |
| 2 | VOLVO | 0 |
| 3 | T6000 | 2 |
| 4 | T800 | 1 |
+--------+----------+---------+
If I edit register T6000 I will receive KENWORTH
and VOLVO
in select, but PadreID equals to 2, so I want a default option will be 2? 如果我编辑寄存器T6000,我将在选择中收到KENWORTH
和VOLVO
,但是PadreID等于2,所以我希望默认选项为2? what I need to change as selected value? 我需要更改为选定值吗? Regards 问候
HTML: HTML:
<select class="form-control" ng-hide="Catalogos.length==0" ng-change="filtro(selected)" ng-model="selected" ng-options="item.Nombre for item in Catalogos "></select>
$scope.selected = $scope.Catalogos[0];
result: 结果:
if you have the id of the needed select item, you can use ng-options="item.Codigo as item.Nombre for item in Catalogos ">
the ng-model
of the select
will hold the Codigo
and the view to the user will show Nombre
如果您具有所需选择项的ID,则可以将ng-options="item.Codigo as item.Nombre for item in Catalogos ">
select
ng-options="item.Codigo as item.Nombre for item in Catalogos ">
的ng-model
将保留Codigo
并且用户视图将显示Nombre
can you try this <select class="form-control" ng-hide="Catalogos.length==0" ng-change="filtro(selected)" ng-model="selected.Codigo" ng-options="item.Codigo as item.Nombre for item in Catalogos"></select>
您可以尝试这个<select class="form-control" ng-hide="Catalogos.length==0" ng-change="filtro(selected)" ng-model="selected.Codigo" ng-options="item.Codigo as item.Nombre for item in Catalogos"></select>
I personally find the syntax ng-options
difficult to read and understand, I've always preferred using ng-repeat
with ng-bind
, ng-value
, and ng-selected
to achieve similar results. 我个人觉得语法ng-options
难以阅读和理解,我一直首选将ng-repeat
与ng-bind
, ng-value
和ng-selected
以达到类似的结果。 Here's how you can accomplish this using an alternative method: 您可以使用替代方法来完成此操作:
<select class="form-control" ng-hide="Catalogos.length==0" ng-change="filtro(selected)" ng-model="selected">
<option ng-repeat="item in Catalogos" ng-bind="item.Nombre" ng-value="item.Codigo" ng-selected="item.ID === object.ID"></option>
</select>
I'm not sure how you're wanting to set which option is selected, but to use ng-selected
, you need any property in the current item
to evaluate to true when compared with another value. 我不确定您要如何设置选择哪个选项,但是要使用ng-selected
,当与另一个值比较时,需要当前item
中的任何属性才能将其评估为true。
ngSelected docs: https://docs.angularjs.org/api/ng/directive/ngSelected ngSelected文档: https ://docs.angularjs.org/api/ng/directive/ngSelected
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.