1

I have html.

<select  ng-model="user.role" class="form-control" name="role" >
    <option value="">Select Role</option><option ng-repeat="role in roles" value="{{role.authority}}">{{role.authority}}</option> </select>

Now roles is a list and role.authority is string. i want to set default selected if role.authority==MYROLE then set to default, which is in role.authority.

How can i do

1 Answer 1

5

You have to assign the same value to select model inside controller, in your case it would be something like this

$scope.user.role = $scope.roles[0].authority

or with use of ngSelected

<option ng-repeat="role in roles" value="{{role.authority}}" ng-selected="role.authority == 'defaultValue'">{{role.authority}}</option>
Sign up to request clarification or add additional context in comments.

5 Comments

its, $scope.roles=data.roles. here roles is list of role object.
In HTML i pass selected = "someValue", but i don't pass it hard coded. i want to check if role.authority contains "SOMEROLE" then set to to default selected.
yes, so after this line you can assign $scope.user.role = $scope.roles[0].authority where 0 is your default role
no its not like that, i want to do like ng-if(role.authority=="ROLENAME") then this is selected, and when i call this it give me selected data. this time i select role to perform action. now can i set something as default from my list after check in ng-if or something like that
can you send me link where you used this please.

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.