0

I have an angular user edit page with a select box:

    <select name="clientRole" ng-model="client.role" required>
        <option value="{{client.role}}">{{client.role}}</option>
        <option ng-repeat="role in roleOptions" value="{{role.name}}">{{role.name}}
    </select>

The array "roleOptions" contains all of the possible roles. This page is used to edit a client, and the client will have a role already assigned. When the page comes up, I want the role that is assigned to the client to show as the default value. The way I have it above, the role is duplicated.

Does angular have a way that handles this, or do I need to edit the "roleOptions" array to delete the client role before the page is called?

2
  • 1
    Use ng-options alongside your select element instead of ng-repeat with the option element. That way your ng-model will implicitly select the default value. Commented Jul 24, 2014 at 2:26
  • @miqid Why don't you write it as an answer? Commented Jul 24, 2014 at 2:27

1 Answer 1

1

Try ng-options instead:

<select name="clientRole" 
        ng-model="client.role" 
        ng-options="role.id as role.name for role in roleOptions" 
        required>     
</select>

In this example, I assume that client.role is your role.id

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

Comments

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.