【问题标题】:AngularJS ng-options get selected item as an objectAngularJS ng-options 将所选项目作为对象
【发布时间】:2014-09-22 12:24:01
【问题描述】:

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

 <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>

谁能帮我解决这个问题?谢谢。

【问题讨论】:

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


    【解决方案1】:

    您只需从 ng-options 中删除 select as 部分,以便选定的 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,其中select 是product.ID,因此您只需将其更改为label for value in array

    【讨论】:

    • 但是当我使用track时它不是默认选择的,在as的情况下选择它,因为我只在集合中存储id(参考)。那么有什么解决办法吗?
    • tks.. 你救救我
    猜你喜欢
    • 1970-01-01
    • 2019-08-15
    • 2014-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-26
    相关资源
    最近更新 更多