3

I am getting empty Option field in select tag.

My code is shown below:

openSelectBoxModel="47"
openSelectList=[{id:""47",name:"text"}];
valuekey="id";
titlekey="name";

<select id="reportOpenSelectBoxSingle" size="6" ng-style='selectStyle' class="openSelectBox" ng-model="openSelectBoxModel" ng-change="changeFn()" >
<option ng-selected="openSelectBoxModel===item[valueKey]" ng-repeat="item in openSelectList" id="" ng-value="{{item[valueKey]}}" ng-title="{{item[titleKey]}}" ng-bind="item[titleKey]"></option>
</select>

Please help me to solve this problem.

1
  • getting<option value="? string:47 ?"></option><option value="47">text</option> Commented Apr 29, 2015 at 10:16

1 Answer 1

1

You should not use ng-selected with ng-model.

The thing to do is to bind the selected item to your ng-model before displaying it.

//Also, instead of ng-repeat, you should use ng-option
As far as performance is regarded : ng-options does not create child scopes for every iteration. When angular performs its digest, your ng-repeat will slow it. If you have a list with hundreds of elements, you will feel a difference.

<select id="reportOpenSelectBoxSingle" 
        size="6" 
        ng-style='selectStyle' 
        class="openSelectBox" 
        ng-model="openSelectBoxModel" 
        ng-change="changeFn()" >
     <option ng-repeat="item in openSelectList" 
             value="{{item[valueKey]}}" 
             title="{{item[titleKey]}}" 
             ng-bind="item[titleKey]">
     </option>
</select>

Furthermore, you need to declare your variables inside a controller :

$scope.openSelectBoxModel="47";
$scope.openSelectList=[{id:"47",name:"text"}];
$scope.valuekey="id";
$scope.titlekey="name";
Sign up to request clarification or add additional context in comments.

2 Comments

if i use ng-option i cannot use title attribute.
I addded a small explanation about ng-repeat vs ng-options.

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.