3

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 a value to $scope.jdeSerials.litm? Is there any better idea to solve this like creating a service which handles this two GETs? I think that reason is the GET method is asynchronous, but what is the best solution to handle situations like this?

EDIT/update

I changed the service call like this:

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

I got the LITM, BUT I got the errormessage as well:

TypeError: Cannot read property 'then' of undefined

2 Answers 2

1

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, call method get from service:

lotnService.get({id:lotn}).$promise.then(
 function(jdeSerials) {
   $scope.jdeSerials = jdeSerials;
   console.log("1--------------->LITM:"+$scope.jdeSerials.litm);
});
Sign up to request clarification or add additional context in comments.

4 Comments

I think that this is not going to help because $resource is offering get, query, post etc. automatically, but I will try it. docs.angularjs.org/api/ngResource/service/$resource
Yes, you are right. The only thing i don't understand is why your app name is the same of your service.
It is just a module name. App name is testClientApp. I got seven service modules introduced in app.js. Thanks for commenting, any idea how to get this working? Still struggling with the same problem and this is making me crazy.
I test an example like your code and all went good. I send a request to server with MyService.get({id:1234}).$promise.the(...) and i get my data back. I review your code many times and i can´t see nothing wrong. Only set $scope.jdeSerials in side of callback function not when you call service get. Cheers
0

What angular js version are you using?

Does the following work ?

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

(without the $promise)

I was browsing the docs and also angular-resource.js source for previous versions and it appears that the synthax has changed somewhere along the line.

On angular-resource.js 1.2.0 source:

The Resource instances and collection have these additional properties:
$promise: the {@link ng.$q promise} of the original server interaction that created this * instance or collection.

On 1.0.8 there is no mention of the $promise propery, however.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.