简体   繁体   English

angularjs-基于先前选择选项的具有嵌套json的ng-options

[英]angularjs - ng-options with nested json based on previous select option

i'm kind of new in Angular, and I facing a problem, I didn't find an answer that could help me, if some one could, thank you. 我是Angular的新手,但我遇到了一个问题,没有找到可以帮助我的答案,如果有人可以的话,谢谢。 :) :)

I have a json like this: 我有一个像这样的json:

"items": [
{
  "post_type": "release",
  "label": "Releases"
},
{
  "post_type": "news",
  "label": "Notícias",
  "options": [
    {
      "id": 1,
      "name": "Galeria de Fotos"
    },
    {
      "id": 2,
      "name": "Agência de Notícias"
    },
    {
      "id": 3,
      "name": "Rádio"
    },
    {
      "id": 4,
      "name": "TV"
    }
  ]
},....

And I m getting data like: 我正在获取类似的数据:

  var _preparingPostTypes = function ($scope) {
    $scope.post_types = [];

    PostTypeService.getPostTypes().then(function (data) {
      $scope.post_types = data.data;
    });
  };

What I want to do is create 2 selects, 1st - with the post_type ('release', 'news') and a second one with the 'options' array from a post_type, that is only visible when select an option with the 'options' in the array like 'news'. 我想要做的是创建2个选择,第一个-使用post_type(“发布”,“新闻”),第二个使用post_type中的“ options”数组,仅当选择带有“ options”的选项时可见就像“新闻”一样 I did something like this, where I can get the post_type like a charm, but I don't know how to proceed: 我做了这样的事情,我可以像魅力一样获得post_type,但是我不知道如何进行:

<div class=form-group>
    <label>Pesquisar em:</label>

    <select title="Pesquisar em:" class="form-control select-post-type"
        ng-model="widget.post_type"
        ng-options="item.post_type as item.label for item in post_types.items"></select>
</div>

EDIT: 编辑:

In my request I need to pass the post_type string to server, from the first select, so the ng-options is: 在我的请求中,我需要从第一个选择中将post_type字符串传递给服务器,因此ng-options是:

ng-options="item.post_type as item.label for item in post_types.items

Not: 不:

ng-options="item as item.label for item in post_types.items

Thanks everybody! 谢谢大家!

You can add an ng-change to the first select that calls a function that loads the options 您可以在第一个选择中添加ng-change,以调用加载选项的函数

<select title="Pesquisar em:" class="form-control select-post-type"
    ng-model="widget.post_type"
    ng-options="item as item.label for item in post_types.items" ng-change="typeChanged()"></select>

And the function would load inside $scope.options the selected type options. 然后该函数将在$ scope.options内部加载选定的类型选项。 Then you iterate over $scope.options in the second select 然后在第二个选择中遍历$ scope.options


UPDATE: 更新:

I haven't tested the code, but it may guide you 我尚未测试代码,但可能会指导您

Select: 选择:

<select title="Options:" class="form-control select-post-type"
      ng-model="widget.option"
      ng-options="option as option.name for option in options">

Change function (triggered when the value of the first select changes, so it will have the responsibility of loading the options of the post_type selected): 更改功能(在第一个选择的值更改时触发,因此它将负责加载所选的post_type的选项):

$scope.typeChanged = function() { 
    for (var i = 0; i < $scope.post_types.items.length; ++i) {
        if ($scope.post_types.items[i].label == $scope.widget.post_type) $scope.options = $scope.post_types.items[i].options || [];
    }
}

widget.post_type should contain the selected item. widget.post_type应该包含所选项目。 So the other select can have options like option as option.name in widget.post_type.options . 因此,另一个选择可以option as option.name in widget.post_type.options具有诸如option as option.name in widget.post_type.options这样的option as option.name in widget.post_type.options Note that you should probably have an options collection on each item if you want this to work. 请注意,如果您希望它起作用,则可能应该在每个项目上都有一个选项集合。

You can do this very easily using following technique. 您可以使用以下技术轻松完成此操作。 You just need to use the ng-model from the first select. 您只需要从第一个选择中使用ng-model。 See my approach: 看我的方法:

<label>Pesquisar em:</label>

<select title="Pesquisar em:" class="form-control select-post-type"
    ng-model="firstSelect"
    ng-options="item as item.label for item in post_types.items"></select>

<select title="Second one" class="form-control select-post-type"
    ng-model="secondSelect" ng-show="firstSelect.options"
    ng-options="option as option.name for option in firstSelect.options"></select>

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

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