无法在Angular JS 2中初始化连续选择

时间:2016-07-12 10:21:43

标签: angularjs

我创建了一个fiddle来展示在选择中依赖于其他选择的问题的问题(我喜欢称之为连续选择)

我无法为连续选择分配初始值。 像
HTML

<select ng-model="car.brand" ng-options="brand for brand in brands" ng-change="selectedBrand(car.brand)"></select>
<select ng-model="car.model" ng-options="model.name for model in cars[carIndex]"></select>
<input type="text" ng-model="car.model.capacity">  

JS

$scope.brands = ['Ford', 'Honda', 'Hyundai', 'Mahindra',
    'Maruti Suzuki', 'Nissan', 'Renault', 'Skoda', 'Tata', 'Toyota', 'Volksvagen'
  ];
$scope.selectedBrand = function(brand) {
    console.log(brand);
    $scope.carIndex = $scope.brands.indexOf(brand);
  }
$scope.cars[0] = [{
    name: "Figo",
    capacity: 45
  }, {
    name: "Ecosport",
    capacity: 52
  }, {
    name: "Fiesta",
    capacity: 45
  }, {
    name: "Endeavour",
    capacity: 71
  }];

/*My Attempt*/
/*Successful*/
$scope.car.brand = $scope.brands[0];
/*Unsuccessful*/
 $scope.car.model = $scope.cars[0][0].name;

问题
如何在使用第一个选择时动态初始化第二个选择和输入框?

1 个答案:

答案 0 :(得分:1)

首先,您应该设置carIndex。并且car.model必须设置为与model

中的<select ng-options=".. for model in ..相同的类型
 $scope.carIndex=0;
 $scope.car.brand = $scope.brands[0];
 $scope.car.model = $scope.cars[0][0];