我有两个动态对象数组。一种用于颜色,一种用于公共汽车。目标是为总线分配颜色。因此,使用 ng-repeat 代码为每种颜色创建选择,然后使用 ng-change 调用 updatebus 函数,传入颜色 ID。但是,它不起作用。颜色 ID 始终未定义。
如何使用两个数组将颜色分配给总线?我究竟做错了什么?
我试着看看这个答案:getting the ng-object selected with ng-change
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - combining two arrays</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.27/angular.min.js"></script>
</head>
<body ng-app="selectExample">
<script>
angular.module('selectExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.colors = [
{name:'black', id:'a'},
{name:'white', id:'b'},
{name:'red', id:'c'},
{name:'blue', id:'d'},
{name:'yellow', id:'e'}
];
$scope.buses = [
{name:'bus 1', colorid:''},
{name:'bus 2', colorid:''}
];
$scope.theBus = $scope.buses[1]; // red
$scope.updatebus = function(bus, id){
//alert(id); // undefined
$scope.theBus = bus;
bus.cid = id;
};
}]);
</script>
<div ng-controller="ExampleController">
<p>
Bus Color:
<div ng-repeat="bus in buses">
{{bus.name}}
<select ng-model="myColor"
ng-options="color.name for color in colors"
ng-change="updatebus(bus, color.id)">
<option value="">-- choose color --</option>
</select>
<p>
</div>
<div>
the bus "{{theBus.name}}" has color id "{{theBus.colorid}}"
</div>
</div>
</body>
</html>