简体   繁体   English

如何获取ngResource查询的内容[对象对象]

[英]How do I get the content of an ngResource query [object object]

I am trying to get the content on an ngresource query method. 我正在尝试在ngresource查询方法上获取内容。

I have a factory like this 我有这样的工厂

    .factory('factoryName', ['$resource','API_URL', function ($resource,API_URL) {
return $resource(
    API_URL+'route1/route2/:userId',{userId: '@_userId'},
    {            
        update: {method: 'PUT'},
        query:{method:'GET'}
    }

);
}])

and my controller looks like this 我的控制器看起来像这样

  .controller('myCtrl',function($scope,$http,factoryName,userService){
   $scope.userId = userService.getUser().userId;
    $scope.result  =factoryName.query({userId:$scope.userId})
   console.log($scope.result)

  }) 

I discovered that,that was logging [object object], meanwhile when I trace the response in the Network, it is executing well and responding with the result I want. 我发现它正在记录[对象对象],与此同时,当我在网络中跟踪响应时,它执行得很好,并以所需的结果进行响应。 but my $scope.result is [object object] . 但是我的$ scope.result是[object object]。 I try endless trick to get the content of the [object object] but I could not. 我尝试了无穷的技巧来获取[object object]的内容,但我做不到。

I have also tried 我也尝试过

    .controller('myCtrl',function($scope,$http,factoryName,userService){
       $scope.userId = userService.getUser().userId;
        factoryName.query({userId:$scope.userId},
         function(response){
        $scope.result = response
       console.log(response)
      })
    }) 

that is not even giving me anything. 那什至没有给我任何东西。

I just realized that, the response are showing up in the view 我只是意识到,响应正在视图中显示

  <div ng-app="myApp">
   <div ng-controller="myCtrl">

      <h2>{{result.something}}</h2>

   </div>
</div>

But, I don't know why it's not logging in the console correctly 但是,我不知道为什么它无法正确登录控制台

You have a few problems in your code. 您的代码中有一些问题。

First one is that angular-resource is asynchronous. 第一个是角度资源是异步的。 Which means that 意思就是

$scope.userId = userService.getUser().userId;
$scope.result  =factoryName.query({userId:$scope.userId})

This will not work. 这是行不通的。

You should do something like this: 您应该执行以下操作:

$scope.user = userService.getUser();
$scope.user.$promise.then(function(user) {
    $scope.result = factoryName.query({userId:user.userId})
})

Angular ng-resource query is asynchronous - and it creates empty array (Instead of loaded data), and provides $promise attribute, which contains promise that is resolved when data load is done. Angular ng-resource查询是异步的-它将创建一个空数组(代替已加载的数据),并提供$ promise属性,该属性包含在完成数据加载后将解决的promise。 Proper way of doing this is 正确的做法是

$scope.result = factoryName.query({userId:$scope.userId});
$scope.result.$promise.then(function() {
    console.log($scope.result);
});

Also, as menotioned in other answer, same can happen on user load 另外,如在其他答案中提到的,用户负载也会发生相同的情况

All calls to a remote server are done in an async way, so you cannot synchronously assign the result to your scoped variable. 对远程服务器的所有调用均以异步方式完成,因此您无法将结果同步分配给作用域变量。 There are two ways to handle this. 有两种方法可以解决此问题。

https://docs.angularjs.org/api/ngResource/service/$resource https://docs.angularjs.org/api/ngResource/service/$resource

Via callback function: 通过回调函数:

$scope.userId = userService.getUser(
  function(user) {
    $scope.userId = user.userId
  }
);

Another (preferred) option is to use the $promise that was later introduced for the $resource service. 另一个(首选)选项是使用后来为$ resource服务引入的$promise

$scope.userId = userService.getUser().$promise.then(
  function(user) {
    $scope.userId = user.userId;
  },
  function(err) {
    $scope.err = err;
  }
);

Your code looks fine to me. 您的代码对我来说很好。 I'm guessing the console is playing a trick on you by calling toString() on your response object before logging it. 我猜控制台通过在记录响应对象之前在其响应对象上调用toString()来欺骗您。

Try to verify the object contents by serializing it: console.log(JSON.stringify($scope.result)); 尝试通过序列化来验证对象的内容: console.log(JSON.stringify($scope.result));

EDIT 编辑

You have to provide query() with callbacks, the first callback is for a successfull operation, the first argument being the queried data, the second callback for unsuccessfull operations. 您必须为query()提供回调,第一个回调用于成功的操作,第一个参数是查询的数据,第二个回调用于不成功的操作。 Here's a fiddle for you :) 这是为您准备的小提琴 :)

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

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