我正在拿一个对象并将其放入'员工'的选择下拉列表中:
$scope.Employees = [{
"id": 1,
"name": "George",
"job": "Janitor"..
..根据选择填充一些数据。效果很好!这是我目前的小提琴:
<select ng-model="selectedOption" ng-options="option.name for option in Employees"></select>
<br>
<br>
<p>ID: {{selectedOption.id}}</p>
<p>JOB: {{selectedOption.job}}</p><br><pre>{{selectedOption}}</pre>
从控制器中您还可以看到“车辆”对象:
$scope.Vehicles = [{
"id": 1,
"employee_id": 1,
"make": "Honda",
"model": "Civic"...
我希望能够以某种方式将其包含在我的下拉选择中。因此,当某人选择某个员工时,他们的车辆将被列出。我真的不知道该怎么做。我知道你可以将两个对象都放入ng-model中并添加一些ng-options。
<select ng-model="selectedOption.Vehicles" ng-options="option.make for option in Vehicles"></select>
..虽然我很确定这是不正确的。任何帮助将不胜感激。如果我需要提供更多信息,请告诉我!
谢谢! Ť
答案 0 :(得分:1)
你可以这样做:
<div ng-controller="MyCtrl">
<select ng-model="selectedOption" ng-options="option.name for option in Employees"></select>
<br>
<br>
<p>ID: {{selectedOption.id}}</p>
<p>JOB: {{selectedOption.job}}</p>
<br><pre>{{selectedOption}}</pre>
<div>
<select class="form-control" ng-model="selectedVehicle" ng-options="v.make for v in Vehicles | filter:{employee_id: selectedOption.id}"></select>
</div>
然后您的JavaScript如下:
var myApp = angular.module('myApp', []);
function MyCtrl($scope) {
$scope.Employees = [{
"id": 1,
"name": "George",
"job": "Janitor"
}, {
"id": 2,
"name": "Frank",
"job": "Scientist"
}, {
"id": 3,
"name": "Julie",
"job": "Florist"
}, {
"id": 4,
"name": "James",
"job": "Teacher"
}];
$scope.selectedOption = $scope.Employees[0];
$scope.Vehicles = [{
"id": 1,
"employee_id": 1,
"make": "Honda",
"model": "Civic"
}, {
"id": 2,
"employee_id": 2,
"make": "BMW",
"model": "M3"
}, {
"id": 3,
"employee_id": 4,
"make": "Nissan",
"model": "Pathfinder"
}, {
"id": 4,
"employee_id": 2,
"make": "Jaguar",
"model": "XF"
}];
}
修改 - Working Fiddle