简体   繁体   English

如何使用AngularJS解析许多json文件

[英]How to parse many json files using AngularJS

I am trying to parse in my HTML page many JSON files: Here is my service's code: 我试图在我的HTML页面中解析许多JSON文件:这是我的服务代码:

app.factory('myapp', ['$http', function($http) {
var tab = ['url1', 'url2', 'url3'] 
for(i=0; i<tab.length; i++){
  return $http.get(tab[i]) 
            .success(function(data) { 
              return data; 
            }) 
            .error(function(err) { 
              return err; 
            }); }
}]);

In my HTML file , I only have the information of the first json file. 在我的HTML文件中,我只有第一个json文件的信息。

Here's my HTML code: 这是我的HTML代码:

<tr>
<td>{{data.nm}}</td>
<td>{{data.cty}}</td>   
<td>{{data.hse}}</td>
<td>{{data.yrs}}</td> 
</tr> 

Is there something to add in my HTML so I can get the information from all the json files or any other solution? 是否有东西要添加到我的HTML中,以便我可以从所有json文件或任何其他解决方案中获取信息?

First off, you return in the first iteration in your for loop, so you only get the data for the first url. 首先,您在for循环的第一次迭代中返回,因此您只获取第一个url的数据。 Don't return right away, assign a scope variable to your data: 不要立即返回,为您的数据分配范围变量:

Factory

app.factory('myapp', ['$http', function($http) {        
    function getLists() {
        var tab = ['url1', 'url2', 'url3'];
        var list = [];
        for(i=0; i<tab.length; i++){
        $http.get(tab[i]) 
            .then(function(res) {
                list.push(res.data);
            });
        }
        return list;
    }

    return {
        getLists: getLists
    };
]);

Controller 调节器

$scope.list = myapp.getLists();

HTML HTML

<tr ng-repeat="d in list">
  <td>{{d.nm}}</td>
  <td>{{d.cty}}</td>   
  <td>{{d.hse}}</td>
  <td>{{d.yrs}}</td> 
</tr> 

I think what you are looking for is $q.all 我认为你要找的是$ q.all

I would solve the problem like this 我会像这样解决问题

angular.module('app').factory('myRequestsFactory',function($http, apiHost) {

    var myRequestsFactory = {};

    myRequestsFactory.geturl1 = function() {
        return $http.get(url1);
    };

    myRequestsFactory.geturl2 = function() {
        return $http.get(url2);
    };

    myRequestsFactory.geturl3 = function() {
        return $http.get(url3);
    };

    return myRequestsFactory;
});

Then I would create an other service 然后我会创建一个其他服务

angular.module('app').factory('helperService',function($q, myRequestsFactory) {

    var helperService = {};

    helperService.GetAll = function() {
      return $q.all([
                      myRequestsFactory.geturl1(), 
                      myRequestsFactory.geturl2(), 
                      myRequestsFactory.geturl3() ]);
      };

    return helperService;
});

Then in my controller .. 然后在我的控制器..

  function loadData() {
        helperService.GetAll().then(
          function(result) {
            $scope.url1result = result[0];
            $scope.url2result = result[1];
            $scope.url3result = result[2];

          });
      },
      function(error) {

      }
    );
  }

That's how i would get access to this data 这就是我将如何访问这些数据

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

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