2003

Displaying JSON data in drop-down in AngularJS

Question:

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

Answer1:

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];

<a href="http://plnkr.co/edit/URgRU52sslPvSHutCmpt?p=preview" rel="nofollow">Demo</a>

Recommend

  • Problems with GET call to rest API
  • Angular page doesn't refresh after data is added or removed
  • Not able to display correct data in table -AngularJS
  • Function calls are not supported. Consider replacing the function or lambda with a reference to an e
  • Meteor helpers not available in Angular template
  • Python CGI os.system causing malformed header
  • Ajax Loaded meta Tags
  • Exchange data b/w iOS devices using Bluetooth 4.0
  • Master page gives error
  • Uncaught Error: Could not find module `ember-load-initializers`
  • Apache 2.4 and php-fpm does not trigger apache http basic auth for php pages
  • How to set/get protobuf's extension field in Go?
  • Sending data from AppleScript to FileMaker records
  • Running a C# exe file
  • 'TypeError' while using NSGA2 to solve Multi-objective prob. from pyopt-sparse in OpenMDAO
  • Symfony2: How to get request parameter
  • ORA-29908: missing primary invocation for ancillary operator
  • How to get next/previous record number?
  • Google cloud sdk not working when python points python3
  • Why winpcap requires both .lib and .dll to run?
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • How to get icons for entities from eclipse?
  • Proper way to use connect-multiparty with express.js?
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do you join a server to an Active Directory (domain)?
  • coudnt use logback because of log4j
  • How does Linux kernel interrupt the application?
  • MATLAB: Piecewise function in curve fitting toolbox using fittype
  • JaxB to read class hierarchy
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal