如何在AngularJS控制器中访问Java类的JSON数组

How to access JSON array of Java class in AngularJS Controller

本文关键字:Java 类的 JSON 数组 访问 AngularJS 控制器      更新时间:2023-09-26

我的java代码中有dto (POJO)数据列表。我将数据列表转换为JSON数组。我想初始化我的AngularJS控制器的$scope.data variable与数据显示在UI。

我在ProjectBean类中的java代码

   JSONArray jsonArray = new JSONArray();
    for (int iterator = 0; iterator < dbProjectListSize; iterator++) {
        JSONObject responseDetailsJson = new JSONObject();            
        responseDetailsJson.put("name", tempChartProject.getText());
        responseDetailsJson.put("color", "#9FC5F8");
        responseDetailsJson.put("from", ChartProject.getStart_date());                                      
        jsonArray.add(responseDetailsJson);
        }

AngularJS控制器
app.controller('Ctrl', ['$scope', function ($scope) {
      $scope.data =projectBean.jsonArray;
}]);

我想用Project bean类中的Json列表初始化$scope.data变量,以便在UI中显示此数据。

请帮帮我。

使用service with controller,并确保你注入了service。

在controller.js

controllersModule.controller('MyCtrl', function($scope, $filter, $http,  **FactoryYouwant**) 
    {
        **FactoryYouwant**.getdata().then (function(responseData){  
            $scope.ResponseFromServer =responseData;
    }
在service.js

controllersModule.factory('**FactoryYouwant**, function($http) {
    var responseData = null;
    return {
        getdata : function(){           
            responseData = "$http.post "; //that gets you data;
            return responseData;
        }
      };
});