简体   繁体   English

在角度上迭代服务返回的JSON

[英]Iterating over JSON returned by a Service on angular

I am trying to iterate over and search by id and return the other values corresponding to the id from a JSON object of the type shown below, by $resource in the controller. 我试图迭代并通过id搜索并从控制器中的$ resource通过下面显示的类型的JSON对象返回与id对应的其他值。 I am not understanding where am I wrong in this case? 在这种情况下,我不明白我错在哪里? Please help! 请帮忙!

This is the controller 这是控制器

appSettings.controller('applistController', ['$scope', 'AppListService',
    function($scope, AppListService){
    // Have to iterate here to search for an id, how?
    // The Above app.json file is returned by the ApplistService(not showing the factory here as it works already.)
        $scope.allapps = AppListService.listAllApps().get();
    // console.log($scope.allapps.data) returns undefined as so does console.log($scope.allapps.length).
    // Where am I wrong?
    }
]);

The JSON is of the type : JSON的类型如下:

{"data":[
    {
      "id":"files_trashbin",
      "name": "TrashBin",
      "licence":"AGPL",
      "require":"4.9",
      "shipped": "true",
      "active":true
    },
    {
      "id":"files_external",
      "name": "External Storage",
      "licence":"AGPL",
      "require":"4.93",
      "shipped":"true",
      "active":true
    }
    ],
  "status":"success"
}

The AppListService.listAllApps().get(); AppListService.listAllApps().get(); returns promise i suppose. 我想是回报承诺。 Sounds like you try to print before got actual data. 听起来像是在获得实际数据之前尝试打印。

I would use following approach: 我会使用以下方法:

var appSettings = angular.module('myModule', ['ngResource']);

appSettings.controller('applistController', ['$scope', 'AppListService',
function($scope, AppListService){

     AppListService.listAllApps()
                        .then(function (result) {
                           $scope.allapp = result;                           
                        }, function (result) {
                            alert("Error: No data returned");
                        });  

}]);


appSettings.factory('AppListService', ['$resource','$q',  function($resource, $q) {

  var data = $resource('somepath', 
         {},
        { query: {method:'GET', params:{}}}
                 );


       var factory = {

            listAllApps: function () {
              var deferred = $q.defer();
              deferred.resolve(data);
             return deferred.promise;
            }

        }
        return factory;
}]);

Here is code that shows the extraction of the id values, based on your json. 这是代码,显示基于您的json提取id值。

var json = '{"data":[{"id":"files_trashbin","name":"TrashBin","licence":"AGPL","require":"4.9","shipped":"true","active":true},{"id":"files_external","name":"External Storage","licence":"AGPL","require":"4.93","shipped":"true","active":true}],"status":"success"}';
$scope.allapps = JSON.parse(json);
$scope.ids = new Array();
var sourceData = $scope.allapps["data"];
for (var i=0; i<sourceData.length; i++) {
    $scope.ids.push(sourceData[i].id);
}

Here is a jsFiddle giving an example of this extraction , integrated with Angular. 这是一个jsFiddle,给出了一个与Angular集成的提取示例

This code assumes that the JSON returned by your service is identical to what you have shown. 此代码假定您的服务返回的JSON与您显示的JSON相同。 Please note - there were initially some extra and missing commas in your JSON text (which I subsequently fixed), which may have also contributed to the errors that you were seeing. 请注意 - 您的JSON文本(我随后修复了)中最初有一些额外的和缺少的逗号,这可能也导致了您看到的错误。

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

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