简体   繁体   English

使用ng-repeat(AngularJS)遍历JSON

[英]Iterate over JSON with ng-repeat (AngularJS)

So I have a JSON file and i want to have 2 select options 所以我有一个JSON文件,我想有2个选择选项

  • First to select the name (eg: 'Dev01') 首先选择名称(例如:'Dev01')
  • Second to select one vlan of the first one 第二个选择第一个VLAN

So this is inside my controller: 所以这是在我的控制器内部:

    $scope.VLANSelection = {};

    $scope.VLANSelection.selectedOption = null;

    $scope.VLANSelection.availableOptions =  [
            {name: 'Prod01', vlans: [
                    {VlanName: 'ProdVLANHome', id: 0},
                    {VlanName: 'ProdVLANOffice', id: 1}
                ]},
            {name: 'Prod02', vlans: [
                    {VlanName: 'Prod02VLANHome', id: 0},
                    {VlanName: 'Prod02VLANOffice', id: 1}
                ]},
            {name: 'Test01', vlans: [
                    {VlanName: 'Test01VLANHome', id: 0},
                    {VlanName: 'Test01VLANOffice', id: 1}
                ]},
            {name: 'Test02', vlans: [
                    {VlanName: 'Test02VLANHome', id: 0},
                    {VlanName: 'Test02VLANOffice', id: 1}
                ]},
            {name: 'Dev01', vlans: [
                    {VlanName: 'Dev01VLANHome', id: 0},
                    {VlanName: 'Dev01VLANOffice', id: 1}
                ]},
            {name: 'Dev02', vlans: [
                    {VlanName: 'Dev02VLANHome', id: 0},
                    {VlanName: 'Dev01VLANOffice', id: 1}
                ]},
            {name: 'sdf', vlans: [
                    {VlanName: 'Tui01VLANHome', id: 0},
                    {VlanName: 'Tui02VLANOffice', id: 1}
                ]},
            {name: 'dsf', vlans: [
                    {VlanName: 'TuiProdVLANHome', id: 0},
                    {VlanName: 'TuiProdVLANOffice', id: 1}
                ]}
        ];

My first selection looks like this: 我的第一个选择如下所示:

<select class="form-control col-md-9" ng-model="VLANSelection.selectedOption" id="SecurityZoneInput">
                <option ng-repeat="option in VLANSelection.availableOptions" ng-value="{{option}}">{{option.name}}</option>
            </select>

And my second selection: 我的第二个选择:

<select class="form-control col-md-9"  id="ProdNameInput">
                <option ng-repeat="vlan in VLANSelection.selectedOption track by $index"  ng-value="{{vlan.VlanName}}">{{vlan.VlanName}}</option>
            </select>

The First selection looks fine, but inside the second selection I have many empty elements, instead of the 2 Vlan names that should be inside. 第一个选择看起来不错,但是在第二个选择内部,我有许多空元素,而不是应该在其中的2个Vlan名称。

So again, if I select 'Prod01' you should see 'ProdVLANHom' and 'ProdVLANOffice' inside the second selection. 再次如此,如果我选择“ Prod01”,则第二个选择内应显示“ ProdVLANHom”和“ ProdVLANOffice”。

Can someone help me? 有人能帮我吗?

Whenever you are using an angular directive , no need to use angular expression binding ng-value="{{option}}" , Instead you need to give like this ng-value="option" 每当您使用angular指令时,都无需使用绑定角度表达式ng-value="{{option}}" ,而是需要这样给ng-value="option"

<select class="form-control col-md-9" ng-model="VLANSelection.selectedOption" id="SecurityZoneInput" ng-options="option.name for option in VLANSelection.availableOptions" ng-value="option">
    </select>

<select class="form-control col-md-9"  id="ProdNameInput" ng-model="selected" ng-options="vlan.VlanName for vlan in VLANSelection.selectedOption.vlans" ng-value="vlan">
    </select>

For clarification, where is the logic that sets VLanSelection.selectedOption? 为了澄清起见,设置VLanSelection.selectedOption的逻辑在哪里?

I see that you instantiated it and set it to null, which coincidently is why it is showing empty elements. 我看到您实例化了它并将其设置为null,这恰巧是它显示空元素的原因。

Try initializing it like this: $scope.VLANSelection.selectedOption = {}; 尝试像这样初始化它:$ scope.VLANSelection.selectedOption = {}; Just the same as you did the Parent. 就像您做父母一样。

Use of ng-options might be more clear and appropriate here 在这里使用ng-options可能更清晰和适当

 <select class="form-control col-md-9" ng-model="VLANSelection.selectedOption" 
    id="SecurityZoneInput" 
ng-options="option as option.name for option in VLANSelection.availableOptions">
 </select>


  <select class="form-control col-md-9" ng-model="test"  id="ProdNameInput" 
ng-options="vlan as vlan.VlanName for vlan in VLANSelection.selectedOption.vlans">
  </select>

 angular.module('mymodule', []); angular.module('mymodule') .controller('myctrl', function($scope) { var vm = this; vm.selectedOption = null; vm.availableOptions = [{ name: 'Prod01', vlans: [{ VlanName: 'ProdVLANHome', id: 0 }, { VlanName: 'ProdVLANOffice', id: 1 }] }, { name: 'Prod02', vlans: [{ VlanName: 'Prod02VLANHome', id: 0 }, { VlanName: 'Prod02VLANOffice', id: 1 }] }, { name: 'Test01', vlans: [{ VlanName: 'Test01VLANHome', id: 0 }, { VlanName: 'Test01VLANOffice', id: 1 }] }, { name: 'Test02', vlans: [{ VlanName: 'Test02VLANHome', id: 0 }, { VlanName: 'Test02VLANOffice', id: 1 }] }, { name: 'Dev01', vlans: [{ VlanName: 'Dev01VLANHome', id: 0 }, { VlanName: 'Dev01VLANOffice', id: 1 }] }, { name: 'Dev02', vlans: [{ VlanName: 'Dev02VLANHome', id: 0 }, { VlanName: 'Dev01VLANOffice', id: 1 }] }, { name: 'sdf', vlans: [{ VlanName: 'Tui01VLANHome', id: 0 }, { VlanName: 'Tui02VLANOffice', id: 1 }] }, { name: 'dsf', vlans: [{ VlanName: 'TuiProdVLANHome', id: 0 }, { VlanName: 'TuiProdVLANOffice', id: 1 }] }]; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <body ng-app="mymodule"> <div ng-controller="myctrl as ct"> <div> {{ct.selectedOption}} </div> <select ng-model="ct.selectedOption" ng-options="item as item.name for item in ct.availableOptions"> <option ng-repeat="option in ct.availableOptions" ng-value="{{option}}">{{option.name}}</option> </select> <select ng-model="ct.selectedOption1" ng-options="item as item.VlanName for item in ct.selectedOption.vlans"> <option ng-repeat="vlan in ct.selectedOption.vlans" ng-value="{{vlan.VlanName}}">{{vlan.VlanName}}</option> </select> </div> </body> 

Just try it using ng-options 只需使用ng-options尝试一下

first select: 首先选择:

<select ng-options="option.name for option in VLANSelection.availableOptions" ng-model="VLANSelection.selectedOption" id="SecurityZoneInput">
  </select>

and second select 第二选择

<select ng-options="vlan.VlanName for vlan in VLANSelection.selectedOption.vlans track by $index" ng-model="VLANSelection.secondSelect" class="form-control col-md-9" id="ProdNameInput">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM