将 JSON 文件存储在 AngularJS 变量中以与'ng-repeat'一起使用



这个项目的目标是在网站上显示Oracle PL/SQL记录。 我使用以下教程 (http://draptik.github.io/blog/2013/07/13/angularjs-example-using-a-java-restful-web-service/) 来设置与数据库的连接。 我能够存储和显示单个记录的值,但在添加更多记录时则无法存储和显示。

Sample JSON Information
[  
 {  "firstName":"FN1",  
    "lastName":"LN1",  
    "email":null,  
    "createdBy":-1,  
    "createdDate":"2013-09-24"  
 },  
 {  "firstName":"FN2",  
    "lastName":"LN2",  
    "email":null,  
    "createdBy":-1,  
    "createdDate":"2013-09-24"  
 },  
 {  "firstName":"FN3",  
    "lastName":"LN3",  
    "email":null,  
    "createdBy":-1,  
    "createdDate":"2013-09-24"  
 },  
 {  "firstName":"FN4",  
    "lastName":"LN4",  
    "email":null,  
    "createdBy":-1,  
    "createdDate":"2013-09-24"  
 },  
 {  "firstName":"FN5",  
    "lastName":"LN5",  
    "email":null,  
    "createdBy":-1,  
    "createdDate":"2013-09-24"  
 }  
]  

该示例使用了一个工厂,我确信它保存了来自 json 的数据,但我无法让它存储超过单个记录的数据。 理想情况下,我将能够按照此示例中的方式循环浏览记录:http://jsfiddle.net/pJ5BR/124/。

我将不胜感激对此的任何建议。 这些是当前工厂的定义方式。

services.js:  
services.factory('QueryFactory', function ($resource) {
    return $resource('/Query/rest/json/queries/get', {}, {
        query: {
            method: 'GET',
            params: {},
            isArray: false
        }
    });
});
controllers.js:  
app.controller('MyCtrl1', ['$scope', 'QueryFactory', function ($scope, QueryFactory) {
    QueryFactory.get({}, function (QueryFactory) {
        $scope.firstName = QueryFactory.firstName;
    });
}]);

QueryFactory.get()的结果不存储在 QueryFactory 中,而是存储在返回的 promise 对象中。另外,您需要使用 query() 而不是 get() ,因为响应是一个数组而不是单个对象。

因此,您的控制器应如下所示:

app.controller('MyCtrl1', ['$scope', 'QueryFactory', function ($scope, QueryFactory) {
    $scope.results = QueryFactory.query();
    // $scope.results is set to a promise object, and is later updated with the AJAX response
}]);

您可以像这样使用 HTML 中的数据:

<ul ng-controller="MyCtrl1">
  <li ng-repeat="result in results">{{result.firstName}}</li>
</ul>

最新更新