T000...">

Reading data from $ resource.get () in AngularJS

JSON / XML from REST

{
  litm: "T00000245",
  lotn: "00004"
}

      


<jdeSerials>
  <litm>T00000245</litm>
  <lotn>00004</lotn>
</jdeSerials>

      

AngularJS Controller

//Searching a product with serial number/LOTN
$scope.searchProduct = function () {
    var lotn = $scope.jdeSerials.lotn;
    console.log("searchProduct---->" + lotn);//log-->searchProduct---->00004


    $scope.JdeSerials = lotnService.get({id: lotn}, function() {
      console.log($scope.jdeSerials);//log-->[object Object] 
      console.log($scope.jdeSerials.litm);//log-->undefined!!!!!

    });

//var litm = $scope.jdeSerials.litm;
//$scope.jdeproduct = productService.get({id: litm});

};

      

AngularJS Service

angular.module('lotnService', ['ngResource'])
    .factory('lotnService', ['$resource',
        function ($resource) {
            console.log('------lotmService-----');
            return $resource(
'http://localhost:8080/RMAServer/webresources/com.pako.entity.jdeserials/:id',
                    {},
                    {

                        update: { method: 'PUT', params: {id: '@lotn'} }
                    });
        }]);

      

Question

How can I get the value for $ scope.jdeSerials.litm? Is there a better idea for solving this problem, such as creating a service that handles these two GETs? I think the reason is that the GET method is asynchronous, but what is the best solution for situations like this?

EDIT / update

I changed the call to the service as follows:

$scope.JdeSerials =   lotnService.get({id:lotn})
.$promise.then(function(jdeSerials) {
    $scope.jdeSerials = jdeSerials;
    console.log("1--------------->LITM:"+$scope.jdeSerials.litm);
 });

      

I have LITM, BUT I got the error too:

TypeError: Cannot read property 'then' of undefined

      

+3


source to share


2 answers


Try to create a get method in your resource.

angular.module('lotnService', ['ngResource'])
  .factory('lotnService', ['$resource', function ($resource) {
     return $resource(  'http://localhost:8080/RMAServer/webresources/com.pako.entity.jdeserials/:id',
                {},
                {
                    get: { method: 'GET', params: {id: '@lotn'}},
                    update: { method: 'PUT', params: {id: '@lotn'} }
                });
    }]);

      



Then, in your controller, the calling method gets from the service:

lotnService.get({id:lotn}).$promise.then(
 function(jdeSerials) {
   $scope.jdeSerials = jdeSerials;
   console.log("1--------------->LITM:"+$scope.jdeSerials.litm);
});

      

+1


source


What version of angular js are you using?

Does the following work?

lotnService.get({id:lotn}).then(
    function(jdeSerials) { ... }
);

      

(without $ prom )



I've been looking through the docs and the angular-resource.js source for previous versions and it looks like the syntax changed somewhere along the line.

On angular -resource.js 1.2.0 source :

The resource instances and collection have the following additional properties
$promise

: {@link ng. $ q prom} the original server communication that created this * instance or collection.

1.0.8 does not mention the $ prom property .

0


source







All Articles