how to use ng-option to set default value of select element
how to use ng-option to set default value of select element
Today we will see how to set the default value of select element in AngularJS ng-option. We can use the ngRepeat on "<option>" elements instead of ngOptions to achieve a similar result. But ngOptions will provide some benefits such as reducing the memory and increasing speed by not creating a new scope for each repeated instance.
[html]
<!-- app.js -->
$scope.degrees = [
{"degree_code":"GB","degree_name":"Bachelor of Science"},
{"degree_code":"GR","degree_name":"Non Degree Undergraduate"}
];
$scope.selecteddegree = {degree_code:"GB"};
<!-- html -->
<select name="degree" class="form-control" ng-model="selecteddegree" ng-options="degree.degree_name for degree in degrees track by degree.degree_code" >
<option value=""> Select Degree </option>
</select>
[/html]
- Here we have JSON object with (key,value) pair format and named as degrees.
- We will use ng-options to bind the "<option > " element of select element with JSON object "degrees" data.
- We have passed an expression in ng-options "degree.degree_name for degree in degrees track by degree.degree_code"
- degree.degree_name - degree is variable name and on each repeat the row data for JSON object will copied to degree variable.
- First repeat the degree contains : " {"degree_code":"GB","degree_name":"Bachelor of Science"} ".
- If want to get the name of degree we can simple use " degree.degree_name"
- If want to get the code of degree we can simple use "degree.degree_code"
- Second repeat the degree contains : " {"degree_code":"GR","degree_name":"Non Degree Undergraduate"} "
- So this "degree.degree_name" goes to display text of the select element.
- degree in degrees - we are specifying copy each row from degrees JSON object to degree variable on each repeat.
- track by degree.degree_code - here I would like to set the option element value attribute with my degree_code.
- <option value="GB">
- <option value="GR">
- If you don't specify the track by expression then ng-option will set the index of the JSON object. Like bellow.
- <option value="0">
- <option value="1">
- <option value="2">
- By default ng-options will set the selected value is 0 indexed value.
- If you want to set the different value as default value then you have to specify manually with the help of ng-model directive.
- We have bind the select element with ng-model and named as selecteddegree.
- Now we can easily set the custom selected element value from our controller.
- $scope.selecteddegree = {degree_code:"GB"};
- $scope.selecteddegree = {degree_name:"Bachelor of Science"}; - it will not work
- Because we set the "<option value="GB">" by using track by expression so it will ng-option will search for Bachelor of Science in option value.
[html]
+ index.html
+ app.js
[/html]
[javascript]
/**
* Created by sinukoll on 4/15/15.
*/
'use strict';
var splessonsApp = angular.module('splessonsApp',[]);
splessonsApp.controller("splessonsController",function($scope){
// working with ng-options with Array data
$scope.degreesarray = ["GB","GR"];
$scope.selecteddegreeobjectarray = "GB";
// working with ng-options with Json data
$scope.degreesobject = [
{"degree_code":"GB","degree_name":"Bachelor of Science"},
{"degree_code":"GR","degree_name":"Non Degree Undergraduate"}
];
$scope.selecteddegreeobject = {degree_code:"GB"};
});
[/javascript]
[html]
<!DOCTYPE html>
<html ng-app="splessonsApp">
<head lang="en">
<meta charset="utf-8">
<title>SPLessons</title>
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" type="text/css">
</head>
<body ng-controller="splessonsController">
<br/>
<br/>
<div class="container">
<div class="jumbotron">
<h3> <a href="http://www.splessons.com/2015/05/angularjs-value-attribute-for-select/" > SPLessons.com </a> </h3>
<form class="form-horizontal">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Degrees in Array: </label>
<div class="col-sm-10">
<select name="degreeinagrray" class="form-control" ng-model="selecteddegreeobjectarray" ng-options="degree for degree in degreesarray" >
<option value=""> Select Degree </option>
</select>
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Degree in Object: </label>
<div class="col-sm-10">
<select name="selecteddegreeobject" class="form-control" ng-model="selecteddegreeobject" ng-options="degree.degree_name for degree in degreesobject track by degree.degree_code" >
<option value=""> Select Degree </option>
</select>
</div>
</div>
</form>
<p> Selected value in Array : {{selecteddegreeobjectarray}}</p>
<p> Selected value in Object : {{selecteddegreeobject.degree_name}}</p>
</div>
</div>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.20/angular.min.js" type="text/javascript" ></script>
<script src="app.js" type="text/javascript"></script>
</body>
</html>
[/html]