我有这样的一个表:使用过滤器,以获得正确的选择的选项
+--------+----------+---------+
| ID | Nombre | PadreID |
+--------+----------+---------+
| 1 | KENWORTH | 0 |
| 2 | VOLVO | 0 |
| 3 | T6000 | 2 |
| 4 | T800 | 1 |
+--------+----------+---------+
与选择像这样的:
<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>
我收取使用功能相仿,
function cargarCatalogo() {
apiService.get("../../api/Catalogo/GetCatalogoPadre/" + $scope.Catalogo + "/",
null,
function(res) {
$scope.Catalogos = res.data;
$scope.selected = $filter('filter')($scope.Catalogos, {
ID: $scope.padreID
}, true);
$scope.filtro($scope.selected);
},
errorCatalogo);
}
我想要选择我的数据库,例如,如果我点击编辑到T6000
它有PadreId = 2
s Ø我选择应该是VOLVO
谁拥有ID = 2
但如果我PadreId = 1
编辑T800选择应该是KENWORTH谁拥有ID = 1
如何正确使用filter
,然后选择选择该选项的选择?
当我安慰它,我得到正确的价值观为:
但我只是用不容$scope.filtro($scope.selected);
有人可以帮助我,请让尽可能默认值?
UPDATE:
$ scope.filtro定义:
$scope.filtro = function(selected) {
$scope.selectedID = selected.ID;
}
更新2
由于Vanojx1评论我用find函数类似这样:
function cargarCatalogo() {
apiService.get("../../api/Catalogo/GetCatalogoPadre/" + $scope.Catalogo + "/",
null,
function(res) {
$scope.Catalogos = res.data;
},
errorCatalogo);
}
$scope.filtro = function(selected) {
$scope.selectedID = selected.ID;
}
$scope.selected = $scope.Catalogos.find(function(catalog) {
return catalog.ID == $scope.padreID;
});
$scope.filtro($scope.selected);
但我得到selected is undefined
,任何人都可以帮助我吗?
$过滤器返回一个数组,你需要的数组中的一个项目'$ scope.selected', 试用 '$ scope.selected = $ filter('filter')($ scope.Catalogos,{ID:$ scope.padreID},true)[0]';你不需要调用'$ scope.filtro($ scope.selected);' – Fetrarij
我试试它,但我只是继续得到白色默认的第一个选项,并且我在$ scope.selected' @FetraR得到了未定义。 – Ledwing
添加$ scope.filtro的定义 – Vanojx1