Working with JSON and XML in AngularJS
{
itemNumber: "T00000245",
lotNumber: "00004"
}
<jdeSerials>
<itemNumber>T00000245</itemNumber>
<lotNumber>00004</lotNumber>
</jdeSerials>
Dealing with Asynchronous Calls in AngularJS
//Searching a product with serial number/LOTN
$scope.searchProduct = function () {
var lotNumber = $scope.jdeSerials.lotNumber;
console.log("searchProduct---->" + lotNumber);//log-->searchProduct---->00004
$scope.JdeSerials = lotnService.get({id: lotNumber}, function() {
console.log($scope.jdeSerials);//log-->[object Object]
console.log($scope.jdeSerials.itemNumber);//log-->undefined!!!!!
});
//var itemNumber = $scope.jdeSerials.itemNumber;
//$scope.jdeproduct = productService.get({id: itemNumber});
};
Creating an AngularJS service for handling API calls
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: '@lotNumber'} }
});
}]);
Question:
How can I access the value of $scope.jdeSerials.itemNumber? Is there a better approach to handling multiple asynchronous GET requests like splitting them into separate services? It seems that the issue is related to the asynchronous nature of the GET method, but what is the optimal solution for managing such scenarios?
EDIT/update:
I updated the service call as follows:
$scope.JdeSerials = lotnService.get({id:lotNumber})
.$promise.then(function(jdeSerials) {
$scope.jdeSerials = jdeSerials;
console.log("1--------------->Item Number:" + $scope.jdeSerials.itemNumber);
});
I was able to retrieve the Item Number, BUT encountered the following error message:
TypeError: Cannot read property 'then' of undefined