我试图了解控制器如何使用级联下拉列表在angularjs中工作。但是我的第二个下拉列表没有根据之前的下拉值填充。
我的JSON结果:
[{
"id": "23031",
"fabricante": "ALFA ROMEO",
"modelo": "33",
"motor": "1.3 \/ 1.3 ie",
"combustivel": "Gasolina \/ GNV",
"ano_vela": "",
"ngk": "",
"ngk_green": "BPR6EY",
"gap": "0,7",
"ano_cabo": "",
"cabos_ngk": "",
"inicio": "",
"fim": "",
"seguimento": "4 RODAS",
"ano_bobina": "",
"bobina_ngk": "",
"pais": "Argentina",
"n_processo": "0"
}, {
"id": "23057",
"fabricante": "ARO",
"modelo": "Serie 10",
"motor": "1.4",
"combustivel": "Gasolina \/ GNV",
"ano_vela": "",
"ngk": "BP6HS",
"ngk_green": "",
"gap": "0,7",
"ano_cabo": "",
"cabos_ngk": "",
"inicio": "",
"fim": "",
"seguimento": "4 RODAS",
"ano_bobina": "",
"bobina_ngk": "",
"pais": "Argentina",
"n_processo": "0"
}]
我的控制器已编辑:
angular.module('starter.controllers', [])
.controller('fabricanteController', ['$scope','$http', function($scope, $http) {
$http.get('JSON_PATH').success(function (data) {
$scope.fabricantes = data;
$scope.$watch('fabricante', function(newVal) {
if (newVal) $scope.modelo = data;
console.log($scope.modelo);
});
});
}]);
我的HTML:
<label class="item item-input item-select">
<div class="input-label">
Fabricante
</div>
<select ng-model="fabricante" ng-options="fab.fabricante for fab in fabricantes track by fab.fabricante">
<option value="" disabled="disabled">Selecionar</option>
</select>
</label>
<label class="item item-input item-select">
<div class="input-label">
Modelo
</div>
<select ng-model="modelo" ng-options="mod.modelo for mod in fabricantes">
<option value="" disabled="disabled">Selecionar</option>
</select>
</label>
试图环顾四周如何让它运转但没有运气。 知道为什么吗?
答案 0 :(得分:0)
你的手表没有射击,因为你传入的是一个字符串,而不是一个变量。另外,我不知道你在哪里检查newVal,但它不会像那样......
基本实施:
$scope.$watch($scope.fabricante, function() {
$scope.modelo = data;
console.log($scope.modelo);
});
使用newVal检查:
$scope.$watch($scope.fabricante, function() {
if($scope.modelo!=data)
{
$scope.modelo = data;
console.log($scope.modelo);
}
});
您还可以在“选择”框中使用ng-change,这将在任何更改时触发。
<select ng-model="fabricante" ng-change="doSomething()" ng-options="fab.id for fab in fabricantes"></select>
答案 1 :(得分:0)
如果要显示所选'modelo'
对象的'fabricante'
属性,则必须将第二个DDL的ng-option
指向新的$scope
数组变量,例如:
$scope.modelos = [{"modelo": newVal.modelo}];
你的ng选项应该是:
<select ng-model="test" ng-options="mod.modelo for mod in modelos">
这是一个有效的JSFiddle
希望这有帮助。