最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Passing value instead of whole object with ng-options - Stack Overflow

programmeradmin0浏览0评论

I'm parsing a .json file and am showing all available options in a select:

<div bo-switch-when="dropdown">
  <fieldset>
      <select ng-options="options.text for option in question.body.options" ng-model="question.answer" ></select>
  </fieldset>
</div>

It is working, but not as I want it to. Instead of getting the whole object to my model, I just want to have the value of this object. Via Chrome Dev Tools:

This object (as in the picture) is in my model. I just want to have the text.

But when i change my ng-options to this:

ng-options="options.text for option.text in question.body.options"

it isn't working at all...

I'm parsing a .json file and am showing all available options in a select:

<div bo-switch-when="dropdown">
  <fieldset>
      <select ng-options="options.text for option in question.body.options" ng-model="question.answer" ></select>
  </fieldset>
</div>

It is working, but not as I want it to. Instead of getting the whole object to my model, I just want to have the value of this object. Via Chrome Dev Tools:

This object (as in the picture) is in my model. I just want to have the text.

But when i change my ng-options to this:

ng-options="options.text for option.text in question.body.options"

it isn't working at all...

Share Improve this question edited May 16, 2014 at 14:10 Ryan 1,0431 gold badge11 silver badges17 bronze badges asked May 16, 2014 at 14:06 ohboy21ohboy21 4,3199 gold badges41 silver badges73 bronze badges 2
  • Tell me... its friday and maybe i am missing something. I have different kind of elements (radio buttons, checkboxes etc.) and my validation works with text, not with whole objects. So i want to change the passing value, not my function in the model. – ohboy21 Commented May 16, 2014 at 14:11
  • @chandu tried to ask you to provide a Plunker, so that we can help you more easily. – sp00m Commented May 16, 2014 at 14:14
Add a comment  | 

2 Answers 2

Reset to default 11

According to ngOptions documentation you can specify the property of the object to be used as options value select as label for (key , value) in object where select is the property binded to the model and label is the property used as options label. No need to ngRepeat here.

In your case just use .text property in both select and label :

 <select ng-options="option.text as option.text for option in question.body.options" ng-model="question.answer" >

This is actually a pretty common question among Angular developers.

You can use ng-repeat, like so:

<select ng-model="question.answer" >
  <option ng-repeat="option in question.body.options" value="{{option.text}}">{{option.text}}</option>
</select>
发布评论

评论列表(0)

  1. 暂无评论