从JSON对象数组返回特定​​对象

时间:2015-07-09 21:08:31

标签: javascript json angularjs angularjs-scope angular-filters

这是我的services.js的样子:

    var app = angular.module('starter.services', [])


    .factory('Studies',function($http,$filter){
        var studies = [];
        $http.get("studies.json").success(
                function(data){
                    //studies = data;
                    angular.copy(data, studies);
                }
            );
        single_object = $filter('filter')(studies, function (d) {return d.nodeRef === "56e3382b-9a76-48ee-9c14-907e71b7a184";})[0];
        console.log(single_object);
      return {
        all: function(){
          return studies;
        }
    };
    })

因为你可以看到我对一个json文件的get请求,该文件包含属性为“nodeRef”的对象恰好是一个属性。我想基于其匹配的noderef过滤掉特定对象。目前console.log返回“undefined”我认为这可能是因为它在json文件加载之前被调用。如果有人能提供解决方案,我将非常感激。

2 个答案:

答案 0 :(得分:1)

您正在执行异步请求并在服务器响应之前触发过滤器,这就是您获得undefined的原因,因为此时您正在将过滤器应用于空数组。

您应该在success块内移动过滤器调用,以使其按预期工作:

var app = angular.module('starter.services',[])

.factory('Studies',function($http,$filter){
    var studies = [];
    $http.get("studies.json").success(
            function(data){
                //studies = data;
                angular.copy(data, studies);
                single_object = filter("56e3382b-9a76-48ee-9c14-907e71b7a184");
                console.log(single_object);
            }
        );
  function filter(node) {
    if (studies.length > 0) {
      return $filter('filter')(studies, function (d) {return d.nodeRef === node;})[0];
    }
  }
  return {
    all: function(){
      return studies;
    },
    filtered: filter
};
})

答案 1 :(得分:0)

你是对的,控制台显示“未定义”,因为尚未在ajax请求之后填充研究。将过滤器和控制台日志移动到.success函数:

$http.get("studies.json").success(
            function(data){
                //studies = data;
                angular.copy(data, studies);
                single_object = $filter('filter')(studies, function (d)  {return d.nodeRef === "56e3382b-9a76-48ee-9c14-907e71b7a184";})[0];
                console.log(single_object);
            }
        );

另外,我认为你需要一个承诺,在完成后更新研究。查看Angular $q

您将返回promise,然后在$ http.success函数内解析它,如下所示:      var deferred = $ q.defer();     .factory( '研究',函数($ HTTP,$ Q,$滤波器){

    var studies = [];
    $http.get("studies.json").success(
            function(data){
                //studies = data;
                angular.copy(data, studies);
                //At this point you can filter data as required, or not at all. I would suggest returning the entire JSON response and not filtering here at all, let the controllers filter the data as needed to maintain a layer of separation between controller and service.

                deferred.resolve(studies);
            }
        );

  return {
    all: function(){
      return deferred.promise;
    }
};
//In your controller, or wherever else...
Studies.all().then(function(studies){ //this will fire when everything is all done. The studies variable is a full JSON object returned from the server (See deferred.resolve() in the service class). Filter here accordingly:
$scope.studies = $filter('filter')(studies, function (d) {return d.nodeRef === "56e3382b-9a76-48ee-9c14-907e71b7a184";})[0];
})