如何使用 ng 选项重写以下语句

How can I rewrite the following statement using ng-options?

本文关键字:语句 重写 选项 何使用 ng      更新时间:2023-09-26

我有以下 Angular/HTML 块,它工作正常,但我的问题是选择后我只能访问定义为 value.id 的所选选项的value,而不是对象数组。

仅使用ng-optionselect上的group by语句重写以下块的最佳实践方法是什么?

<select name="device[ [[$index]] ]" ng-model="selectedModel" ng-change="loadModelImage(selectedModel, $index)">
    <option value="">Model</option>
    <optgroup ng-repeat="model in manufacturerModels" label="[[model.model]]">
        <option ng-repeat="variant in model.variants" value="[[model.id]]">[[model.model + ' - ' + variant.memory + ' - ' + variant.colour]]
    </optgroup>
</select>
    <select ng-model="modelSelected"
           ng-options="model.SubArray.name group by model.type 
                       for model in manufacturerModels" 
           value="model.id"> 
    </select>

看,ng-model modelSelected 将分配所选选项,它将返回您传递的对象类型的对象。
model.SubArray.name 将是您要显示的字符串,例如:"[[mode.model + variant.memory + variant.color",我建议您使用这三个值创建一个字段,然后,而不是 model.name,而是将创建的新变量,例如 model.createdFieldWithThreeNames
然后,您可以按类型,名称,年份以及数据之间的任何匹配项进行分组。
,最后在要使用的数组中,在本例manufacturerModels

向我们发布一些反馈,看看您是否得到了它。