2016-07-29 84 views
0

我JSON这样AngularJS从数组2获得JSON数据在阵列1

{ 
    "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中加载选定车型的零件。例如,如果我选择在DROPDOWN1“本田”,DROPDOWN2应装有:

DROPDOWN2 
tyre 
muffler 

如果我选择在DROPDOWN1“VW”,然后DROPDOWN2将不会加载,因为有大众对于任何部件。

我有HTML 2选择(下拉菜单)是这样,请注意,我用的NG选项而非重复

<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; 

,那我也函数来获得选择的车,目的是要还加载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中?

+0

您不会将数据串联起来使用它。当你这样做时,看下一行在控制台中抛出的错误 – charlietfl

+0

我在上面查看$ scope.myData。谢谢 – pixel

+0

拼写?在你的ng-options中“partin”而不是“partin”? – o4ohel

回答

0

修复了原帖。我在上面的ng选项中使用“part in car.part”而不是“part in part”。这解决了这个问题。