Angularjs, select Box create blank option at first always

Viewed 325

I got some problems with selectbox.

In my original code, I receive an object through $http calling, and that is like below.

[
    {key:'user_id', value:'ID'},
    {key:'user_name', value:'Name'},
    {key:'user_gender', value:'Gender'},
    {key:'user_phone', value:'Phone'},
    {key:'user_email', value:'Email'},
    {key:'user_birth', value:'Birthday'},
];

When I tried to put values of this object into option, it made 'blank option' automatically.

To solve this, I saw two posts

Angular JS Remove Blank option from Select Option

Why does AngularJS include an empty option in select?

but any solutions of these could not help me.

How can I fix this?

Here's my fiddle.

Select box create blank option

I'll wait some handsome or pretty developers who will solve my problem. :)

2 Answers

Try This

HTML Code -

<select ng-options="opt as opt.value for opt in searchOpt.option" ng-model="searchOpt.selected">
    </select>

The ngOptions attribute can be used to dynamically generate a list of elements for the element using the array or object obtained by evaluating the ngOptions comprehension expression

Working Jsfiddle

<select ng-model="itemSelected">
  <option
      ng-repeat="item in data" value="{{ item.key }}"
      ng-selected="{{item.key === itemSelected}}">
    {{ item.value }}
  </option>
</select>

https://plnkr.co/edit/QthDhkvku8UvcVHaWcGG?p=preview

Check the code. Use ng-selected in your option for selected first element.

Related