简体   繁体   English

如何绑定json数据以在js下拉列表?

[英]how to bind json data to drop down in angular js?

I have json array like here 我有像这样的json数组

$scope.parentChartId = [
{
    "id": 1,
    "chart_name": "pieChart",
    "sub_chart_query_param": "id,part,name"
},
{
    "id": 2,
    "chart_name": "pieChart1",
    "sub_chart_query_param": "id,part,name"
},
{
    "id": 3,
    "chart_name": "pieChart2",
    "sub_chart_query_param": "id,part,name"
}]

I want show in select-menu(drop down values like) 我想在选择菜单中显示(下拉类似的值)

1-pieChart1,
2-pieChart2,
3-pieChart3

Is there any way to do it. 有没有办法做到这一点。 any one please help me 任何人请帮助我

Styles for dropdown (edit) 下拉样式(编辑)

<style>
.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    padding: 12px 16px;
}

.dropdown:hover .dropdown-content {
    display: block;
}
</style>

you can call like this 你可以这样打电话

<div class="dropdown">
  <span>Mouse over me</span>
  <div class="dropdown-content">
    <p ng-repeat="child in parentChartId">{{child.id}}-{{child.chart_name}}</p>
  </div>
</div>

This is how you do it. 这就是你的做法。 Simply repeat over the list. 只需在列表上重复即可。

That said, I will suggest you read documentation on select to get a better picture of what is going on and get the best solution to your problem. 就是说,我建议您阅读有关选择的文档,以更好地了解正在发生的事情并获得针对您问题的最佳解决方案。

 angular.module('app', []).controller('ctrl', function($scope){ $scope.parentChartId = [ { "id": 1, "chart_name": "pieChart", "sub_chart_query_param": "id,part,name" }, { "id": 2, "chart_name": "pieChart1", "sub_chart_query_param": "id,part,name" }, { "id": 3, "chart_name": "pieChart2", "sub_chart_query_param": "id,part,name" } ] }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app" ng-controller="ctrl"> <select> <option ng-repeat="item in parentChartId" value="{{item.id}}"> {{item.id + "-" + item.chart_name}}</option> </select> </div> 

Demo https://jsfiddle.net/nuwanniroshana/pgyj95pd/ 演示 https://jsfiddle.net/nuwanniroshana/pgyj95pd/

HTML markup HTML标记

<select ng-options="item as item.id + ' - ' + item.chart_name for item in parentChartId track by item.id" ng-model="selected"></select>

Angular Implementation 角度实现

(function(angular) {
  angular.module('myApp', [])
    .controller('mainController', function($scope) {

      $scope.parentChartId = [{
        "id": 1,
        "chart_name": "pieChart",
        "sub_chart_query_param": "id,part,name"
      }, {
        "id": 2,
        "chart_name": "pieChart1",
        "sub_chart_query_param": "id,part,name"
      }, {
        "id": 3,
        "chart_name": "pieChart2",
        "sub_chart_query_param": "id,part,name"
      }];
    });

})(window.angular)
<div ng-app="myApp" ng-controller="myCtrl">

<select>
<option data-ng-repeat="p in parentChartId" data-ng-value="{{p.id}}" data-ng-bind="p.id + ' - ' + p.chart_name"></option>
</select>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.parentChartId = [
                         {
                           "id": 1,
                           "chart_name": "pieChart",
                           "sub_chart_query_param": "id,part,name"
                         },
                         {
                           "id": 2,
                            "chart_name": "pieChart1",
                            "sub_chart_query_param": "id,part,name"
                         },
                         {
                          "id": 3,
                           "chart_name": "pieChart2",
                           "sub_chart_query_param": "id,part,name"
                         }
                         ]
});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM