【问题标题】:Angulajs Nested loop of select option from array从数组中选择选项的Angularjs嵌套循环
【发布时间】:2020-08-14 10:17:48
【问题描述】:

您好,我正在使用 angularjs 构建一个表单,我有一个这样的 php 返回的数组,我想从该数组构建一个这样的表单。

<b>Company</b>
<select ng-model="attributs[]">
 <option>Chinese</option>
 <option>Japanese</option>
 </select>
 
 <b>Color</b>
<select ng-model="attributs[]">
 <option>Black</option>
 <option>Red</option>
 </select>
[
  {
      id: 1,
      name: "Company",
      attribute_values:[
        {
            id: 1,
            name: "Chinese"
        },
        {
            id: 2,
            name: "Japanese"
        }
    ]
  },
  {
      id: 2,
      name: "Color",
      attribute_values:[
        {
            id: 4,
            name: "Black"
        },
        {
            id: 5,
            name: "Red"
        }
    ]
  }
]

我想要从选择选项中选择的所有值,我尝试了这个但没有工作。提前谢谢你。

<div ng-repeat="attrib in attributes.data">
  <label class="col-sm-5 col-form-label"><% attrib.name %></label>
   <div class="col-sm-5">                                       
     <select ng-model="attrib.attributeSelected" ng-options="atb.value as atb.name for atb in attrib.attribute_values">
    </select>
    </div>
 </div>

【问题讨论】:

    标签: angularjs multiple-select


    【解决方案1】:

    你没有提到选择后你想要的输出格式。 我做了一个变通方法,它会在每个属性内创建一个属性selectedOption,以将所选选项的值保存在相应的属性中。

    <div ng-repeat="attrib in data">
      <label class="col-sm-5 col-form-label">{{attrib.name}}</label>
       <div class="col-sm-5">                                       
         <select ng-model="attrib.selectedOption">
           <option default selected disabled value="">Select</option>
           <option ng-repeat="x in attrib.attribute_values" value={{x.id}}>{{x.name}}</option>
        </select>
        </div>
     </div>
    

    属性selectedOption 将插入如下,

    [{
        "id": 1,
        "name": "Company",
        "attribute_values": [{
            "id": 1,
            "name": "Chinese"
        }, {
            "id": 2,
            "name": "Japanese"
        }],
        "selectedOption": "1"
    }, {
        "id": 2,
        "name": "Color",
        "attribute_values": [{
            "id": 4,
            "name": "Black"
        }, {
            "id": 5,
            "name": "Red"
        }],
        "selectedOption": "5"
    }]
    

    【讨论】:

    • 输出格式完美但 {{selectedOption}} 未显示所选值。
    • 所选数据仅显示在 ng-repeat="attrib in attributes.data" 中,因此我无法将这些数据与表单提交一起传递,任何帮助将不胜感激
    • 我终于找到了解决方案它被推送到属性数组
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-24
    • 1970-01-01
    • 2021-04-04
    • 2019-10-19
    • 2016-06-07
    • 2013-03-27
    • 1970-01-01
    相关资源
    最近更新 更多