0

I am new to Angularjs.I am trying to set default value for select in angularjs with static values.I am not using any ng-repeat or options.But i am not able to set default value using ng-selected.Everywhere i find solutions only for ng-options but not for static values.

Here is my html

    <select name="gender" id="gender" data-ng-model="gender" ng-
    selected=="Male">
    <option value="">Select Gender</option>
    <option value="Male">Male</option>
    <option value="Female">Female</option>
    </select>

Can anyone tell where i am doing wrong?

4 Answers 4

2

You can set default value for ng-model in controller, try adding this in your controller

$scope.gender = 'Male';
Sign up to request clarification or add additional context in comments.

2 Comments

It works.But is it not possible with ng-selected then?
ng-selected directive will be used only in <option> tag not in <select>
2

You can use ng-init directive. As Sraven said , ng-selected must use <option> tag.

My Example

<select name="gender" id="gender" data-ng-model="gender" ng-init="gender='Male'">
    <option value="">Select Gender</option>
    <option value="Male">Male</option>
    <option value="Female">Female</option>
</select>

1 Comment

Still not selecting
2

According to official DOCS, ng-selected is for options not for select.

You should have the boolean as ng-selected value

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body ng-app="">

<select ng-init="mySel = true">
  <option>Select</option>
  <option value="male" ng-selected="mySel">Male</option>
  <option value="female">Female</option>    
</select>

</body>
</html>

Please check this example using ng-model

The preferred way of doing this is using ng-model without ng-selected

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body ng-app="myApp" >


<select name="gender" id="gender" ng-model="gender" ng-controller="myCtrl">
    <option value="">Select Gender</option>
    <option  value="Male">Male</option>
    <option value="Female">Female</option>
  </select>




<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
     $scope.gender = "Male" 
});
</script>

</body>
</html>

4 Comments

@Sachin Hr, ng-selected is for options
I dont want checkbox.On load only i want to display
@SachinHR, updated my answer, you can either take scope or ng-init
@SachinHR, If we use ng-model there is no need to use ng-selected it works even without it. check my second example in my answer.
1

ng-selected directive can be used only in <option> tag

Check the below code

  <select name="gender" id="gender" data-ng-model="gender" >
    <option value="">Select Gender</option>
    <option ng-selected="true" value="Male">Male</option>
    <option value="Female">Female</option>
    </select>

2 Comments

This is the one i was looking for.Thank you very much :)
If we use ng-model there is no need to use ng-selected it works even without it. check my second example in my answer.

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.