admin管理员组文章数量:1392007
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
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
Share Improve this question edited Jun 27, 2014 at 14:09 Yoshi 54.7k14 gold badges92 silver badges107 bronze badges asked Jun 27, 2014 at 13:33 ShekkarShekkar 2443 gold badges10 silver badges22 bronze badges 7- I think your data is not correct. Because you have same names of each key so it is getting overwritten by other one. So in last there is one key with just value E. – Mritunjay Commented Jun 27, 2014 at 13:49
- Thank you ,just now i edited the JSON object structure ,but the name is mon for my requirement . – Shekkar Commented Jun 27, 2014 at 14:11
- No I am not saying to remove name field. But before that you had 4 name fields in one object. which is not possible. – Mritunjay Commented Jun 27, 2014 at 14:12
- yah ,i edited the JSON structure ,thank you – Shekkar Commented Jun 27, 2014 at 14:20
- I think the ments which helped you are seems important for your question. You should vote them, so they will appear in starting of the post and will be helpful for other viewers. Thanx – Mritunjay Commented Jun 28, 2014 at 3:09
1 Answer
Reset to default 4Your 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
本文标签: javascriptDisplaying JSON data in dropdown in AngularJSStack Overflow
版权声明:本文标题:javascript - Displaying JSON data in drop-down in AngularJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744767769a2624145.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论