AngularJS 从 array1 中的 array2 获取 JSON 数据

AngularJS getting JSON data from array2 in array1

我有JSON这样的

{
    "cars":[
        {
            "id":"012",
            "model":"honda",
            "parts":[
                {
                    "id":"p1",
                    "name":"tyre",
                },
                {
                    "id":"p2",
                    "name":"muffler",
                }
            ]
        },
        {
            "id":"022",
            "model":"vw",
            "parts":[

            ]
        }
    ]
}

我是 AngularJS 的新手,我有 2 个下拉菜单,DROPDOWN1 显示的车型如下:

DROPDOWN1
honda
vw

和 DROPDOWN2 应该在 DROPDOWN1 中加载 selected 汽车模型的零件。比如我在DROPDOWN1中select"honda",DROPDOWN2应该加载:

DROPDOWN2
tyre
muffler

如果我 select "vw" 在 DROPDOWN1 中,那么 DROPDOWN2 将不会加载,因为没有 VW 的零件。

我有 html 和 2 个 select(下拉菜单),请注意我使用的是 ng-options 而不是 重复:

<p><strong>Select a car</strong></p>
<select 
    required
    ng-change="selectCarAction()"
    ng-model="selectCar" 
    ng-options="car.model for car in cars track by car.id">
  <option value="" label="Select a car"></option>  
</select>

<p><strong>Select a part</strong></p>
<select 
    required
    ng-model="selectPart" 
    ng-options="part.name for part in parts track by part.id">
  <option value="" label="Select a part"></option>  
</select>

在我的 js 文件中,我得到了我的汽车响应数据,例如:

      $scope.myData= JSON.stringify(myresponse.data);
      $scope.cars= myData.data.cars; 

,然后我有获得 selected 汽车的功能,目的是在我获得 selected 汽车后也加载 DROPDOWN2:

$scope.selectCarAction = function(){
  if ($scope.selectCar == null){
    console.log("You selected car: N/A");
  } else {
    console.log("You selected car: " + $scope.selectCar.model);
    $scope.parts= $scope.selectCar.parts;    
  }
};

虽然我的 DROPDOWN1 加载正常,但我的 DROPDOWN2 从未加载,但在上面的 js 函数中,我的 $scope.parts 获取了部分。如何将这些加载到我的 DROPDOWN2 中?

修复了原始 post。我在上面的 ng-option 中使用 "part in car.part" 而不是 "part in parts" 。这解决了问题。