简体   繁体   中英

How to get value at the ng-model for the options under ng-repeat

Let my HTML is as follows:

<div ng-repeat="option in options">
<select  ng-model="myselect">  
     <option value="">{{option.attribute_text}}:</option>
     <option value="">{{option.attr_value}}-${{option.cost}}</option>
</select>
</div>

I want to get the values of the option selected in the ng-model. Is there any methods to get the values in the ng-model under a ng-repeat ?

if you want to use select you can use ng-select

if you specify ng-model for ng-select it bind the select option to ng-model

you can see it in this jsbin

Have a look at: https://docs.angularjs.org/api/ng/directive/select

Here's an example that should be about right:

Options stored as list of hashes in your controller:

   $scope.options = [ { "attribute_text" : "whatever", "data" : { "value" : 9, "cost" : 8 }, { ... } ]
   // Selected value
   $scope.optionSelected = {};

HTML:

<div>
   <select ng-select="option.attribute_text for option in options" ng-model="optionSelected">
</div>

You have multiple selects and you are giving same model to all of them. you can't give them same myselect to all of them.

give each option(the ones you are repeating) a name, and use an object's keys for their model.

<div ng-repeat="option in options">
  <select  ng-model="selections[option.name]">  
     <option value="">{{option.attribute_text}}:</option>
     <option value="">{{option.attr_value}}-${{option.cost}}</option>
  </select>
</div>

then you can access them like $scope.selections["some option name you gave earlier"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM