1

I have a dropdown that gets its items from database. The default value to be selected in this dropdown is retrieved from a cookie. The problem is,
1. there is an empty option tag as the first option and also,
2. the default value is not selected.
There is no issue at all when I use v1.3.16. This happens when using v1.2.0. (I have to use only v1.2.0)
How to fix this.

ASPX:

<select id="ddlItems" data-ng-model="ItemId">
 <option value="-1">-- All Circles --</option>
 <option data-ng-repeat="item in Items" value="{{item.AreaCode}}" 
  data-ng-selected="item.AreaCode==ItemId">{{item.AreaName}}</option>
</select>

Ctrl.js:

var promise = MyFactory.GetItems();
promise.then(function (success) {
 if (success.data != null && success.data != '') {
  var data = success.data;
  $scope.Items = data.lstItems; //bind items to dropdown      
  $scope.ItemId = itemIdFromCookie; //select default value
  alert(itemIdFromCookie); //it pops -1                       
 }
 else {
  console.log(success.data);
 }
}

Rendered HTML:

<select id="ddlItems" data-ng-model="ItemId">
 <option value="? string:"-1" ?"></option>
 <option value="-1">-- All Circles --</option>
 //...other options
</select>
2
  • @ErikE.I'm sure, because I have posted a working code. Commented Dec 18, 2015 at 3:41
  • I tried your way. Doesnt bind items at all. Commented Dec 18, 2015 at 3:46

2 Answers 2

1

Try to use ngOptions instead

Like this

<select id="ddlItems" 
     data-ng-model="ItemId" 
     ng-options="item.AreaCode as item.AreaName for item in Items">
 <option value="-1">-- All Circles --</option>
</select>
Sign up to request clarification or add additional context in comments.

2 Comments

That did not work. There is still an emtpy option tag and the first option '--All Circles--' is removed.
can you provide fiddle ?? @Qwerty
1

This happens becausedata-ng-model="ItemId" is empty when your model is App is Loaded.

To Have some initial value. you can put default value in ng-init

For Ex : data-ng-init='data-ng-model="--SELECT--";'

Or per your Array list Item which you are getting from database set one of the values.

Remember init will get triggered b/f you complete the Ajax Call.

3 Comments

I get the items & also the desired ItemId from the ajax call. Could you please suggest, how I proceed in this case.
After items are bind to the select set data-ng-model value.
Can you please jsfiddle it

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.