javascript - 使用angular js将json结构转换为下拉列表

标签 javascript angularjs json

我有一个具有属性 categories[] 的 json,在每个 categories 中都有一个 subCategories[];对于每个subCategories,都有一个subSubCategories。我想根据给定的 json 结构生成下拉菜单。

{
  "categories": [
    {
      "men": "Mens",
      "subCat": [
        {
          "topWear": "Top Wear",
          "subSubCat": [
            {
              "tshirts": "T-shirts",
              "otherAttributes": [
                {
                  "fitting": [
                    "type 1",
                    "type 2",
                    "type 3"
                  ]
                },
                {
                  "washCare": [
                    "wash Care 1",
                    "wash care 2"
                  ]
                }
              ]
            },
            {
              "shirt": "Shirt",
              "otherAttributes": [
                {
                  "fitting": [
                    "type 4",
                    "type 5",
                    "type 6"
                  ]
                },
                {
                  "washCare": [
                    "wash Care 4",
                    "wash care 5"
                  ]
                }
              ]
            }
          ]
        },
        {
          "bottomWear": "Bottom Wear"
        }
      ]
    },
    {
      "women": "Women",
      "subCat": [
        {
          "topWear": "Top Wear",
          "subSubCat": [
            {
              "tshirts": "T-shirts",
              "otherAttributes": [
                {
                  "fitting": [
                    "w-type 1",
                    "type 2",
                    "type 3"
                  ]
                },
                {
                  "washCare": [
                    "w-wash Care 1",
                    "wash care 2"
                  ]
                }
              ]
            },
            {
              "shirt": "Shirt",
              "otherAttributes": [
                {
                  "fitting": [
                    "w-type 4",
                    "type 5",
                    "type 6"
                  ]
                },
                {
                  "washCare": [
                    "w-wash Care 4",
                    "wash care 5"
                  ]
                }
              ]
            }
          ]
        },
        {
          "bottomWear": "Bottom Wear"
        }
      ]
    },
    {
      "kids": "Kids"
    }
  ]
}

例如:

  • 最初,第一个下拉菜单应填充menswomenkids 选项。
  • 接下来在主要类别选择中,第二个下拉菜单应填充 subCat 选项。
  • 接下来,在选择 subCat 时,第三个下拉菜单应填充 subSubCat 选项。

如何使用 angular 实现此目的?

最佳答案

首先,您的示例不起作用。而且您的结构不适合这种情况。我为您创建了一个示例:FIDDLE

你可以像这样使用它:

html

<div ng-controller="DemoCtrl" ng-app="main">
    <hr>
    <select ng-model="selectedFirst" ng-options="first.name for first in categories track by first">
    <option value="">Select Account</option>
  </select>
  <select ng-model="selectedSecond"  ng-options="second.name for second in selectedFirst.categories track by second">
    <option value="">Select Account</option>
  </select>
</div>

js

(function () {
  'use strict';

  /**
   * Main module of the main
   */
  angular
    .module('main', []);
})();


(function(){
    'use strict'

  angular
    .module('main')
    .controller('DemoCtrl', DemoCtrl);

  /** @ngInject */
  function DemoCtrl($scope) {
    $scope.categories = [
        {
        "name" : "Mens",
        "categories" : [
            {
                "name" : "Top Wear",
                "categories" : [
                    {

                    }
                ]
            }
        ]
      }
    ];
  }
})();

关于javascript - 使用angular js将json结构转换为下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48057051/

相关文章:

javascript - 如果我在我的 MVC 4 应用程序中删除了 "jquery.unobtrusive-ajax.min.js"那么会发生什么?

javascript - 如何使用 angularfire 将输入与 firebase 中的用户相关联?

javascript - 如何在没有双向绑定(bind)的情况下将 ngModel 传递给 AngularJS 组件?

javascript - Controller 中的 Angular 构造函数

json - 阻止用户对 mongodb 进行 Json 查询

javascript - 如何通过两个路由运行两个 Controller ?

javascript - 检查表单中的多个 div,如果 div 没有指定的类,则发出警报并阻止提交

用于将 .ini 文件转换为 .json 文件的 Javascript 库(客户端)

javascript - 正则表达式不能正常工作

json - 如何将嵌套的 JSON 转换为 scala 中的映射对象