i am new in angular js and learning it. today i got a code to populate dropdown with JSON using angular. here is the code.
<select ng-model="selectedTestAccount" ng-options="item.Id as item.Name for item in testAccounts">
<option value="">Select Account</option>
</select>
angular code
angular.module('test', []).controller('DemoCtrl', function ($scope, $http) {
$scope.selectedTestAccount = null;
$scope.testAccounts = [];
$http({
method: 'GET',
url: '/Admin/GetTestAccounts',
data: { applicationId: 3 }
}).success(function (result) {
$scope.testAccounts = result;
});
});
this is not clear to me. ng-options="item.Id as item.Name for item in testAccounts" why as is using between two field name. if i need to specify 3 fields then code would look like ng-options="item.Id as item.Name as item.Desc for item in testAccounts"
please help me to understand ng-options
also tell me why this selectedTestAccount required ?
this way i populate dropdown but first time a empty row is getting added in dropdown.....why it is happening do not understand.
second issue is when i select country from dropdown then country id is not showing. here is my code. please have a look and guide me.
<div ng-controller="DemoCtrl" ng-app="main">
<p>populate select options with ajax</p>
<select name="cboCountry" ng-model="selectedCountry">
<option ng:repeat="facility in chooseCountries" value="{{facility.id}}">{{facility.name}}</option>
</select>
<span>Selected country id is {{selectedCountry.countryId}}</span>
</div>
var app = angular.module('main', []);
app.controller('DemoCtrl', function ($scope) {
$scope.chooseCountries=[
{countryId : 1, name : "France - Mainland", desc: "some description" },
{countryId : 2, name : "Gibraltar", desc: "some description"},
{countryId : 3, name : "Malta", desc: "some description"}
];
$scope.selectedCountry = angular.copy($scope.chooseCountries[0]);
});
thanks