【发布时间】:2016-05-19 08:47:45
【问题描述】:
我正在尝试根据从db/json file 抓取的数据创建一个输入类型select。
我遇到的问题是我可以选择 Make 部分,但是当我选择一个项目时,所有其他部分都没有更新。
请帮帮我。
下面是我想要实现的截图:
HTML 标记:
<select required ng-change="onCarChange(b,car)" ng-model="reportData.car.make" ng-options="bb.make for bb in cars" class="form-control" >
<option value="">--Select--</option>
</select>
<select required ng-model="reportData.car.model" ng-change="onModelChange(b,model)" ng-options="cha.model.name for cha in b.selectedModels" class="form-control" >
<option value="">--Select--</option>
</select>
<select required ng-model="reportData.car.year" ng-change="onYearChange(b,year)" ng-options="t for t in b.selectedYears" class="form-control" >
<option value="">--Select--</option>
</select>
<select required ng-model="reportData.car.color" ng-change="onColorChange(b,color)" ng-options="t for t in b.selectedColors" class="form-control" >
<option value="">--Select--</option>
</select>
APP.JS 控制器
$scope.onCarChange=function(b,car){
b.selectedModels=car.model;
}
$scope.onModelChange=function(b,model){
b.selectedModel=model;
b.selectedYears=model.years;
}
$scope.onYearChange=function(b,year){
b.selectedYear=year;
b.selectedColor=years.colors;
}
$scope.onColorChange=function(b,color){
b.selectedColor=color;
}
样本数据
$scope.cars = [
{
"model": [
{
"year": [
"2016",
"2015",
"2014"
],
"name": "CTS",
"color": [
{"id":1, "hexvalue":"#000", "description":"Black"},
{"id":2, "hexvalue":"#fff", "description":"White"},
{"id":3, "hexvalue":"#FAF0BE", "description":"Blonde"},
{"id":4, "hexvalue":"#0000FF", "description":"Blue"},
{"id":5, "hexvalue":"#808080", "description":"Grey"}
],
"id": 0
},
{
"year": [
"2016",
"2015",
"2014",
"2013",
"2012"
],
"name": "ATS",
"color": [
{"id":1, "hexvalue":"#000", "description":"Black"},
{"id":2, "hexvalue":"#fff", "description":"White"},
{"id":3, "hexvalue":"#FAF0BE", "description":"Blonde"},
{"id":4, "hexvalue":"#0000FF", "description":"Blue"},
{"id":5, "hexvalue":"#808080", "description":"Grey"}
],
"id": 1
},
{
"year": [
"2016",
"2015",
"2014"
],
"name": "XTS",
"color": [
{"id":1, "hexvalue":"#000", "description":"Black"},
{"id":2, "hexvalue":"#fff", "description":"White"},
{"id":3, "hexvalue":"#FAF0BE", "description":"Blonde"},
{"id":4, "hexvalue":"#0000FF", "description":"Blue"}
],
"id": 2
}
],
"make": "CADILLAC",
"picture": "http://placehold.it/32x32",
"index": 0,
"_id": "573bef46573891a64081d4d6"
}
}
]
【问题讨论】:
-
您传递给所有控制器函数的 b 参数是什么?
-
嗨@MikeFeltman。
b我指的是当你是一个 ng-repeatng-repeat="b in cars"。我没有在这里使用它,因为我不是在重复输入,而是在选项上。你认为我应该从我的代码中删除它吗? -
select & ng-options 不像 ng-repeat 那样工作。尝试使用 ng-model 值作为参数,如下所示:
标签: javascript angularjs angularjs-ng-repeat html-select ng-options