An ng-option is select directive will dynamically added data from the list of items. Array or object value will iterate one by one and bind the <option> tag value and label. ng-option is very useful for select tag implement.
HTML :
<div ng-app="Opt">
<div ng-controller="OptCtrl">
<span>Name :</span>
<span>
<select ng-options="country as country.name for country in countrys track by country.id" ng-model="yourCountry">
<option value="">-- Select Country --</option>
</select>
<br>
<span>Code :</span>
<select ng-options="code for code in codes" ng-model="yourCode">
<option value="">-- Select Code --</option>
</select>
</span>
</div>
</div>
angular JS:
var app = angular.module('Opt',[]);
app.controller('OptCtrl', function($scope){
$scope.countrys = [{"id":1, "name":"Afghanistan"},{"id":2, "name":"Afghan"},
{"id":3, "name":"Albania"},
{"id":4, "name":"Algeria"},
{"id":5, "name":"American Samoa"},
{"id":6, "name":"Angola"},
{"id":7, "name":"Anguilla"}];
$scope.yourCountry= "";
$scope.codes = ["01","02","03","04","05","06"];
$scope.yourCode = "";
});
Result:
Demo
HTML :
<div ng-app="Opt">
<div ng-controller="OptCtrl">
<span>Name :</span>
<span>
<select ng-options="country as country.name for country in countrys track by country.id" ng-model="yourCountry">
<option value="">-- Select Country --</option>
</select>
<br>
<span>Code :</span>
<select ng-options="code for code in codes" ng-model="yourCode">
<option value="">-- Select Code --</option>
</select>
</span>
</div>
</div>
angular JS:
var app = angular.module('Opt',[]);
app.controller('OptCtrl', function($scope){
$scope.countrys = [{"id":1, "name":"Afghanistan"},{"id":2, "name":"Afghan"},
{"id":3, "name":"Albania"},
{"id":4, "name":"Algeria"},
{"id":5, "name":"American Samoa"},
{"id":6, "name":"Angola"},
{"id":7, "name":"Anguilla"}];
$scope.yourCountry= "";
$scope.codes = ["01","02","03","04","05","06"];
$scope.yourCode = "";
});
Result:
Demo
Comments
Post a Comment