简体   繁体   中英

How to access JSON array of Java class in AngularJS Controller

I have list of DTOs(POJO) data in my java code. I am converting that data list to JSON array. I want to initialize my $scope.data variable of AngularJS controller with that data to show in UI.

My java code in ProjectBean class

   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 Controller

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

I want to initialize the $scope.data variable with the Json list from the Project bean class, to display this data in UI.

please help me.

Use service with controller and Make sure you inject service .

in controller.js

controllersModule.controller('MyCtrl', function($scope, $filter, $http,  **FactoryYouwant**) 
    {

        **FactoryYouwant**.getdata().then (function(responseData){  
            $scope.ResponseFromServer =responseData;
    }

in service.js

controllersModule.factory('**FactoryYouwant**, function($http) {
    var responseData = null;

    return {
        getdata : function(){           
            responseData = "$http.post "; //that gets you data;
            return responseData;
        }

      };
});

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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