0

I have an array with the strings (not objects) And I binded them to a Select control using AngularJS But I faced with a problem to set up a default setting because my array contains the strings, not the objects. How can I do it in the most correct way?

This is what I got:

 var genders =["Male", "Female"];

<select data-ng-options="o.Name for o in genders track by o.Id"
 data-ng-model="selectedCustomer.Gender"
</select>
1
  • I'm not sure if understood correctly. Maybe you set default to the first item in the array. For example: var default = genders[0]. In your case default variable would became "Male" Commented Jan 25, 2016 at 12:40

1 Answer 1

1

Read DOCs, You can simply use the expression label for value in array for arrays as data-source.

<select data-ng-options="o for o in genders" 
        data-ng-model="selectedCustomer.Gender">
</select>

You don't need trackexpr

DEMO

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

1 Comment

@J.Doe, See jsfiddle.net/satpalsingh/vn9x52yc Try to avoid using strong words when asking for help

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.