Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
151
rated 0 times [  154] [ 3]  / answers: 1 / hits: 9206  / 10 Years ago, fri, june 27, 2014, 12:00:00

my document.json file contains data like



[
{name :B},
{name :A},
{name :D},
{name :E}
]


when i try to display the json data in drop-down list, the last element E only displayed in drop down .my html file like



<select ng-model=selectedTestAccount ng-options=c as c.name for c in testAccounts></select>


and my script file like



sampleApp.controller('DemoCtrl', function ($scope, $http) {
$scope.selectedTestAccount = null;
$scope.testAccounts = [];

$http.get('document.json').success(function (data) {
$scope.testAccounts = data;
});
});


How can i display this document.json data in drop-down list with default selected as first element in AngularJS. Any guidelines please


More From » json

 Answers
1

Your JSON isn't quite right. You need to have braces around each item, like this:



[ 
{ name :B },
{ name :A },
{ name :D },
{ name :E }
]


You can select the first item in the dropdown by default like this:



$scope.selectedTestAccount = $scope.testAccounts[0];


Demo


[#44272] Thursday, June 26, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lexusg

Total Points: 718
Total Questions: 106
Total Answers: 104

Location: Palestine
Member since Tue, Jul 20, 2021
3 Years ago
;