0

我正在使用选择的角度来列出选项。但是如何将 ng-options 用于嵌套数组?

<select chosen
option="countries"
 ng-model="data"
  ng-options="??">
</select>

这是我的阵列。

 $scope.data=  [
  {
"Name": "Cat1",
"CategoryID": "1",
"actions": [
  {
    "ActionName": "action1",
   },
  {
    "ActionName": "action2",

  }
]
},
{
"Name": "cat 2",

"actions": [
  {
    "ActionName": "action3",
   },
  {
    "ActionName": "action4",

  }
]
}
  {
"Name": "cat 3",
"actions": [
  {
    "ActionName": "action5",
   },
  {
    "ActionName": "actions 5",

  }
 ]
 }

 ]

我想按类别名称列出选项组位于这个
cat1
action1
action2
cat2
action3
action4
cat3
action5
action6

4

2 回答 2

0

您可以解析“数据”并将其转换为以下格式,并使用如下所示的 ng-options。

$scope.countries = [
    { actionName: 'action1', catName: 'cat1' },
    { actionName: 'action2', catName: 'cat1' },
    { actionName: 'action3', catName: 'cat2' },
    { actionName: 'action4', catName: 'cat2' },
    { actionName: 'action5', catName: 'cat3' },
    { actionName: 'action6', catName: 'cat3' }   
];

<select ng-model="country" 
        ng-options="item.actionName group by item.catName for item in countries">
</select>

在此链接中查找有关 ng-options 的更多信息https://www.undefinednull.com/2014/08/11/a-brief-walk-through-of-the-ng-options-in-angularjs/

于 2016-11-23T00:42:32.133 回答
0

我在 javascript 中创建了一个新数组并通过 ng-options 进行了迭代,请查看以下链接

https://plnkr.co/edit/i9LfV50Y2gXElFzVgFDl?p=preview

我创建了一个数组并将所有值推入其中,它工作正常

      $scope.uniqueOptions = [];

for (var i = 0; i < $scope.data.availableOptions.length; i++) 
   $scope.uniqueOptions.push($scope.data.availableOptions[i].Name);
   //alert($scope.data.availableOptions[i].Name);
    for (var j = 0; j < $scope.data.availableOptions[i].actions.length; j++) 
     // alert($scope.data.availableOptions[i].actions.length);
       //alert($scope.data.availableOptions[i].actions[0].ActionName);
            $scope.uniqueOptions.push($scope.data.availableOptions[i].actions[j].ActionName;

enter code here
于 2016-11-23T01:01:30.110 回答