我有這樣的一個表:使用過濾器,以獲得正確的選擇的選項
+--------+----------+---------+
| 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