AngularJS ng options select element as object

I have an array of objects in my scope and I list them in the drop down menu as shown below.

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

I also want to get selectedItem as an object to access the other attributes of the object so that I can manupulate the content on my page. For instance:

 <a ng-show="selectedItem.isAvailable">Buy Now!</a> 

Can anyone help me with this? Thanks.

+5
source share
1 answer

You just need to remove the select as part from the ng options, so the selected product will be ngModel, selectedItem .

Try: -

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

Your current syntax is select as label for value in array where select is product.ID , so you just change it to label for value in array

+7
source

Source: https://habr.com/ru/post/1203134/


All Articles