我的 document.json 文件包含如下数据
[
{"name" :"B"},
{"name" :"A"},
{"name" :"D"},
{"name" :"E"}
]
当我尝试在下拉列表中显示 json 数据时,最后一个元素 E
仅显示在下拉列表中。我的 html 文件如
<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;
});
});
我如何在下拉列表中显示此 document.json 数据,默认选择为 AngularJS 中的第一个元素。请有任何指导方针
最佳答案
您的 JSON 不太正确。您需要在每个项目周围加上大括号,如下所示:
[
{ "name" :"B" },
{ "name" :"A" },
{ "name" :"D" },
{ "name" :"E" }
]
您可以像这样默认选择下拉列表中的第一项:
$scope.selectedTestAccount = $scope.testAccounts[0];
关于javascript - 在 AngularJS 中的下拉列表中显示 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24453174/