1

Below is my select with ng-options:

<select ng-options="key as value for (key , value) in getList() ..."> </select>

The getList() function retrieves the following object:

{"CURRENT_ACCOUNT":"test1","ORDINARY_ACCOUNT":"test2"}

and if I inspect the generated options, it is the following:

<option label="test1" value="string:CURRENT_ACCOUNT" selected="selected">test1</option>

As you can see, everything seems right except the value attribute has a string: prefix.

How can I remove the prefix? What did I do wrong?

3
  • Hello! Do you mean "AngularJS" or "Angular"? AngularJS refers to versions 1.x and lower, and Angular refers to versions 2+. Commented Feb 13, 2019 at 18:06
  • 1
    Looking at the html markup provided for the select, i'm fairly certain this is AngularJS, as ng-options isn't valid in Angular (v2+). But good question and lets get the tags updated properly. Commented Feb 13, 2019 at 18:18
  • Yes, it is AngularJS (1.x) . Commented Feb 14, 2019 at 15:01

1 Answer 1

2

You didn't do anything wrong and your code is actually correct. This is how AngularJS creates the options.

<select ng-model="selected" ng-options="key as value for (key, value) in getList()"></select>
Selected: {{selected}}

If you print out the value of the selected item immediately following it, you'll noticed that the value is just CURRENT_ACCOUNT and not string:CURRENT_ACCOUNT as the option leads you to believe.

Here is a plunker demonstrating it.

https://next.plnkr.co/edit/lvEi6pknJZMh0wab?open=lib%2Fscript.js&deferRun=1

Sign up to request clarification or add additional context in comments.

5 Comments

yes, this is right, printing the variable there are not any problem. the issue is caused by the form that is submitted and the "string:" prefix remain in the attribute value. is there any way to escape to haver this prefix?
So you're submitting the form via the html? You should instead be doing that using Angular to handle your form submissions via ajax requests (see $http.post(...)).
I'm doing the $http.put(... but the value come to the server with "string:" . any idea?
How are you serializing the form data to be sent to the server? Let me update the plunker with a little bit more code to demonstrate how to do so. Should just be grabbing the object from angular.
Issue was related to another JS function that is used to prepare the form in case of kana characters. Thank you very much for helping!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.