显示JSON数据在AngularJS下拉 [英] Displaying JSON data in drop-down in AngularJS
本文介绍了显示JSON数据在AngularJS下拉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我document.json文件包含像
my document.json file contains data like
[
{"name" :"B"},
{"name" :"A"},
{"name" :"D"},
{"name" :"E"}
]
当我尝试在下拉列表中显示JSON数据,最后一个元素电子
只显示在下拉。我的html文件,如
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>
和像
sampleApp.controller('DemoCtrl', function ($scope, $http) {
$scope.selectedTestAccount = null;
$scope.testAccounts = [];
$http.get('document.json').success(function (data) {
$scope.testAccounts = data;
});
});
我怎么能在默认情况下选定为AngularJS第一要素下拉列表显示该document.json数据。任何准则,请
How can i display this document.json data in drop-down list with default selected as first element in AngularJS. Any guidelines please
推荐答案
您的JSON是不完全正确。你需要有各地的每个项目支撑,是这样的:
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];
这篇关于显示JSON数据在AngularJS下拉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文