AngularJS ng-options 将所选项目作为对象

标签 angularjs angularjs-ng-options angularjs-ng-model

我的范围中有一个对象数组,我将它们列在如下所示的下拉控件中。

 <select ng-model="selectedItem" ng-options="product.ID as product.Name for product in products">
     <option value="">Please select a product...</option>
 </select>

我还希望将 selectedItem 作为一个对象来访问该对象的其他属性,以便我可以在我的页面中操作内容。例如;
<a ng-show="selectedItem.isAvailable">Buy Now!</a>

谁能帮我这个?谢谢你。

最佳答案

您只需要删除 select as来自ng-options的部分,以便选中product将是 ngModel,selectedItem .

尝试:-

 <select ng-model="selectedItem" ng-options="product.Name for product in products track by product.id">
     <option value="">Please select a product...</option>
 </select>

您当前的语法是 select as label for value in array哪里selectproduct.ID ,因此您只需将其更改为 label for value in array

关于AngularJS ng-options 将所选项目作为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25974045/

相关文章:

javascript - 很难使用 Angular 向 JSON 文件发出发布请求

javascript - 如何将智能表 `st-search`与ng-model集成?

angularjs - 如何在离开 ui-state 时停止 $interval?

javascript - Angular 查找,多个类

javascript - 使用 ngRepeat 和 ng-src 时看不到要上传的图像预览

angularjs - 在 AngularJS 中使用 ng-options 添加两个选项来选择列表顶部和底部

angularjs - 无法设置 ng-options 的选定值

javascript - Angularjs 空选择器

javascript - AngularJS Ionicframework无限滚动GET REQUEST列表一次10个

angularjs - ng-selected 不适用于选择框中的 ng-model - Angularjs