2016-01-13 57 views
0

我有兩個選擇元素綁定到角度模型。第一個應顯示進程列表,第二個顯示屬於該進程的變量列表。我的數據模型是這樣的:使用angularjs恢復鏈接選擇

"processes": [ 
    { 
     "name": "proces1", 
     "variables": [ 
      "var1", 
      "var2" 
     ] 
    }, 
    { 
     "name": "proces2", 
     "variables": [ 
      "var3", 
      "var4" 
     ] 
    } 
] 

我選擇的結果需要結束了在「滑塊」對象的範圍「滑塊」數組:

$scope.sliders = 
[ 
    { 
    process : "process1", 
    tag  : "var1", 
    } 
] 

我已經實現了選擇如下,靈感來自this jsfiddle。

<tr ng-repeat="slider in sliders track by $index"> 
    <td><select name="processSelect" ng-model="slider.process" ng-options="process.name for process in slider.processes"></select></td> 
    <td><select name="variableSelect" ng-model="slider.tag"  ng-options="v for v in slider.process.variables"></select></td> 
</tr> 

該方法的工作原理,但是如果我的模型已經填充只有變量選擇被選中。進程選擇不是。這是因爲processSelect使用了一個進程字典列表而不是字符串(存儲在模型中)。

我該怎麼做才能做到這一點?一個人通常如何完成角度?

回答

0

我固定這通過稍微改變我的數據模型:

"processes": { 
    "process1": [ 
     "var1", 
     "var2" 
    ], 
    "process2": [ 
     "var3", 
     "var4" 
    ] 
} 

然後我改變了NG-選項:

<tr ng-repeat="slider in sliders track by $index"> 
<td><select name="processSelect" ng-model="slider.process" ng-options="key as key for (key, value) in processes"></select></td> 
<td><select name="variableSelect" ng-model="slider.tag" ng-options="value for (key, value) in processes[slider.process]"></select></td> 
</tr> 

基本上遵循上ng-options角文檔。