【问题标题】:Iterate JSON array in angular controller在角度控制器中迭代 JSON 数组
【发布时间】:2015-11-13 21:47:16
【问题描述】:

我正在尝试遍历 Json 中的数组并在前端显示值。我已经给出了我的代码,但我无法弄清楚我到底错在哪里。我无法检索数组中的值(startDate,endDate)并在前端显示它们。

JSON

"reportTypeObligationTypes": [{
    "reportTypeId": null,
        "consolidationScopeId": 4009,
        "startDate": "2014-01-01",
        "endDate": "9999-12-31",
        "frequencyCode": "Q",
        "reportTypeLabel": null,
        "reportTypeCode": null,
        "consolidationScopeCode": "C",
        "frequencyLabel": "Quarterly"
}]

角度控制器

xxxApp.controller('reportListController', function ($scope, $http, $location, $routeParams) {


    var service_url = "/mdmservice/services/reportTypeEntities/" + $routeParams.id;
    $http.get(service_url).success(

    function (data, status, headers, config) {
        $scope.reportTypeEntities = data;
        angular.forEach($scope.reportTypeEntities, function (item) {
            console.log(item.reportTypeObligationTypes);
        })
    });

    $scope.gridOptions = {
        paginationPageSizes: [25, 50, 100, 200, 300, 400, 500, 1000],
        paginationPageSize: 25,
    };
    $scope.gridOptions.data = 'reportTypeEntities';
    $scope.gridOptions.enableFiltering = true;
    $scope.gridOptions.enableGridMenu = true;
    $scope.gridOptions.fastWatch = true;

    $scope.gridOptions.columnDefs = [{
        name: "entityName",
        width: "35%",
        cellTemplate: '<div style="margin-left: 5px;">' + '  <a href="#edit/{{row.entity.entityId}}">{{row.entity.entityName}}</a>' + '</div>'
    }, {
        name: "entityCode",
        width: "15%"
    }, {
        name: "codeType"
    }, {
        name: "Entity Type",
        field: "entityType.entityTypeName"
    }, {
        name: "reportingId"
    }, {
        name: "Country",
        field: "country.countryName"
    }, {
        name: "startDate",
        field: "reportTypeObligationTypes.startDate"
    }, {
        name: "endDate",
        field: "reportTypeObligationTypes.endDate"
    }, {
        name: "consolidationScopeCode",
        field: "reportTypeObligationTypes.consolidationScopeCode"
    }, {
        name: "frequencyLabel",
        field: "reportTypeObligationTypes.frequencyLabel"
    }, {
        name: "Delete",
        cellTemplate: '<div style="margin-left: 5px;">' + '  <a href="#delete/{{row.entity.entityId}}" class="glyphicon glyphicon-trash btn btn-danger"> Delete</a>' + '</div>',
        enableFiltering: false,
        enableSorting: false
    }];

})

HTML 页面 - Angular UI 网格

<div ng-app="xxxApp" ng-controller="reportListController">

<p class="heading">Entities with Reporting Obligation</p>

<!-- Entities list data table using - 'ui-grid' module  -->
<div ui-grid="gridOptions" class="myGrid" ui-grid-pagination
    ui-grid-selection ui-grid-resize-columns ui-grid-move-columns
    ui-grid-cellNav ui-grid-exporter></div>

【问题讨论】:

  • 你能使用一个指向函数的绑定,并有一个函数根据你拥有的 JSON 返回一个数组吗?
  • 请把html代码也放上去!
  • 有错误信息吗?通常使用 Angular,我们使用 ng-repeat 在 html 中显示数组数据。
  • 我也添加了我的html代码。尼克,我没有任何错误信息。

标签: javascript json angularjs


【解决方案1】:

在您的控制器中,分配$scope.reportTypeObligationTypes = [{JSON}]; 您的标签或指令前:

<div ng-repeat="item in reportTypeObligationTypes">
  <div>{{item.startDate}}</div>
  <div>{{item.endDate}}</div>
</div>

'item' 将代表数组中的每个 JSON 对象,您可以使用 item.(property)。引用每个对象属性。

编辑:尝试:

$scope.gridOptions = {
  data: $scope.myData,
  columnDefs: [
    { name: 'field1', displayName: 'pretty display name' },
    { name: 'field2', visible: false }
 ]
};

【讨论】:

  • 这应该适用于 SGN 发布的内容,听起来他是 Angular 新手。
  • 嗨 Shinobi,我在我的 html 中使用 Angular UI 网格。
  • ui-grid APi 说你应该先将数据集合设置为$scope.data = reportTypeObligationTypes,然后再设置$scope.gridOptions.data = $scope.data 看上面的编辑
猜你喜欢
  • 2020-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-16
  • 2020-12-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多