1

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

2
  • Please post your json data as well Commented Apr 4, 2016 at 10:57
  • at bottom i post json data with code again. have a look please. Commented Apr 4, 2016 at 11:00

4 Answers 4

3

you can use code like also

<select ng-model="selectedTestAccount">
 <option value="">Select Account</option> 
<option ng-repeat="item in testAccounts" value="item.Id">{{item.Name}}    
</option></select>

you want use more field you can use like

<select ng-model="selectedTestAccount">
 <option value="">Select Account</option> 
<option ng-repeat="item in testAccounts" value="item.Id">
  {{item.Id}}-{{item.Name}}
</option></select>

This selectedTestAccount is work same like "Id" in javascript which help to get selected value

Sign up to request clarification or add additional context in comments.

5 Comments

i test your code and set selectedTestAccount this way but did not work $scope.selectedTestAccount = $scope.chooseCountries[0].countryId; no country data is selected when dropdown render......what is missing in your code. give me jsfiddle.
thanks it is working now. tell me when ng-value="x.countryId" set selected country this way $scope.selectedCountry = $scope.chooseCountries[1].name then how it is working because i am refering name but ng-value refer to country id. help me to understand. thanks
i change a bit to select country this way but this line is not working ` $scope.selectedCountry = $scope.chooseCountries[1].countryId.toString(); ` what error is there?
I tried with this two change ng-value="x.countryId" and $scope.selectedCountry = $scope.chooseCountries[1].name but i got empty row Please execute changes properly. how its working
do not understand what r u trying to say? please help me to identity what is the error?
2

try like this.

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 = $scope.chooseCountries[0].countryId;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-controller="DemoCtrl" ng-app="main">
  <select ng-model="selectedCountry" ng-options="item.countryId as item.name for item in chooseCountries">
    <option value="">Select Account</option>
</select>  
</div>

4 Comments

do not understand this line 'item.countryId as item.name for item in chooseCountries'. why we need to specify field name with as keyword because we use as keyword for specify alias in sql. guide me plzz
It is similar to DataTextfield and DataValueField in C#.DataTextField is the one what it will be displayed in dropdownlist and DataValueField is what value you will get on selection in code that we may require to implement some bussiness logic.In this case,item.name is the value you see in the dropdown list and item.id is the datatext field which is used for manipulating business logic.
What if my I have json value like this [{ "FRIDAY": [{ "ID": 1, "Name": "xyz" }] }] and I want to pick Name ? I tried as your method by FRIDAY.Name; but my data is coming null ?
But your data structure is different with OP's data structure. what you want exactly?
0

it may because id and name is field in item-array in testAccounts of scope from response to get request. try to debug set breakpoint in controller logic you get clear idea.

Comments

0

try this

<div ng-controller="DemoCtrl" ng-app="main">
  <select ng-model="selectedCountry">
<option value="">Select Account</option>
      <option ng-repeat="x in chooseCountries" value="{{x.countryId}}"  >{{x.name}}</option>
</select>  
</div>

And script code

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 =  $scope.chooseCountries[1].countryId.toString();   
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.