简体   繁体   English

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

[英]Store Json file in AngularJS variable to use with 'ng-repeat'

The goal of this project is to display Oracle PL/SQL records in a web site. 该项目的目标是在网站上显示Oracle PL / SQL记录。 I have used the following tutorial ( http://draptik.github.io/blog/2013/07/13/angularjs-example-using-a-java-restful-web-service/ ) to set up a connection with the database. 我已使用以下教程( http://draptik.github.io/blog/2013/07/13/angularjs-example-using-a-java-restful-web-service/ )建立与数据库的连接。 I was able to store and display values for a single record, but not when more records were added in. 我能够存储和显示单个记录的值,但是不能添加更多记录。

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"  
 }  
]  

The example uses a factory, which I am convinced is holding the data from the json, but I can't get it to store more than the single record. 该示例使用了一个工厂,我确信该工厂保存着json中的数据,但是我不能让它存储比单个记录更多的数据。 Ideally, I would be able to cycle through the records the way they do in this example: http://jsfiddle.net/pJ5BR/124/ . 理想情况下,我可以按照本示例中的方式浏览记录: http : //jsfiddle.net/pJ5BR/124/

I would appreciate any suggestions with this. 我将不胜感激与此有关的任何建议。 These are how the factory is defined currently. 这些是工厂的当前定义方式。

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;
    });
}]);

The result of QueryFactory.get() is not stored in QueryFactory, but rather stored in a returned promise object. QueryFactory.get()的结果不存储在QueryFactory中,而是存储在返回的Promise对象中。 Also, you need to use query() instead of get() , because the response is an array and not a single object. 另外,您需要使用query()而不是get() ,因为响应是一个数组而不是单个对象。

So your controller should look like this: 因此,您的控制器应如下所示:

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
}]);

You can use the data in your HTML like this: 您可以像这样使用HTML中的数据:

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

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

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