Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I am new to AngularJS and have this following piece of code that I need to edit.

 scope.findData = [];
 scope.clientId;
 scope.identityDocuments_a = [];


scope.getClientIdentityDocuments_a = function(clientID){  
        resourceFactory.clientResource.getAllClientDocuments({clientId: clientID, anotherresource: 'identifiers'}, function(data){
                scope.identityDocuments_a = data;
            });            
        };

        scope.findOptions = function(value){
            var i;
            var deferred = $q.defer();
resourceFactory.clientResource.getAllClientsWithoutLimit({displayName: value, orderBy : 'id', officeId : scope.group.officeId, sortOrder : 'ASC', orphansOnly : true}, function (data) {
                deferred.resolve(data.pageItems);
                  for (i = 0; i <= data.pageItems.length; i++) {

                 scope.clientId = data.pageItems[i].id;
                 scope.getClientIdentityDocuments_a(scope.clientId);
                    //<- I want to access scope.identityDocuments_a here
                }
                scope.findData = data.pageItems;

              });
            return deferred.promise;
        };

Both these functions are under the same controller. I looked at Accessing $scope variable from another method in the same controller Angularjs but it doesn't seem to work. Where am I going wrong? Thanks in advance.

share|improve this question
    
Which variable are you not able to access? The clientID in getClientIdentityDocuments_a()? If that's the problem, try clientId: scope.clientID – Beartums Jan 30 at 5:57
    
I am unable to access scope.identityDocuments_a. I tried alert(scope.identityDocuments_a.toSource()); and it just showed [] – coderGeek Jan 30 at 5:59

Okay, I see the problem. Your getClientIdentityDocuments_a() is using a callback function. THe asynchronous data retrieval happens seperately and so the value is not set by the time you want to use it. You can fix this by returning a promise

scope.getClientIdentityDocuments_a = function(clientID){
        var defer = $q.defer();  
        resourceFactory.clientResource.getAllClientDocuments(
                  {clientId: clientID, 
                   anotherresource: 'identifiers'}, function(data){
                scope.identityDocuments_a = data;
                defer.resolve(data);
            });            
            return defer.promise;
        };

and then, to use the data in your second function:

scope.getClientIdentityDocuments_a(scope.clientId).then(function(documents) {
       // at this point, the scope.identityDocuments_a should be available,
       // but you could just set it here, since the document variable is
       // returning the same thing
    console.dir(scope.identityDocuments_a) // should have the returned data
    console.dir(documents)                 // should be the same as above
});
// Here, the documents will NOT be available, because this executes before
// the promise is resolved
console.dir(scope.identityDocuments_a) // [] or undefined

UPDATE: To clarify, if in getClientIdentityDocuments_a you were to directly assign the variable, such as

scope.getClientIdentityDocuments_a = function(clientID){
        scope.identityDocuments_a = some_document;
};

You would not need the promise and your code would work. But since you are retrieving the data from an asynchronous source, your second function is trying to use the data before the value has been set.

share|improve this answer
    
Hi. thanks for your answer. But I don't quite follow what is scope.getClientIdentityDocuments_a(scope.clientId).then(function(document) { }); for? – coderGeek Jan 30 at 6:21
    
I've turned the getClientIdentityDocuments_a() function into a promise that only resolves after the asynchronous call which updates the identityDocuments_a variable is completed. If you do anything with the variable before the asynchronous call has returned its data, the variable will be unchanged. the .then() syntax can be used with any promise in lieu of callback syntax, and it just waits until told the promise is resolved (in this case by the defer.resolve()) before it executes. – Beartums Jan 30 at 6:25
    
I have tried your code but unfortunately, it doesn't work. I tried alert(scope.identityDocuments_a.toSource()); before the return statement and it showed []. – coderGeek Jan 30 at 6:29
    
Yes, it would show that, because the value has not yet been returned from the resource. When working with asynchronous retrievals, the code continues chugging away while the retrieval is happening. The return defer.promise; is happening immediately after kicking off the data retrieval, before the data has been set. Is there any chance you can give me enough code that I can create a working fiddle? – Beartums Jan 30 at 6:33
    
Sorry..I can't share any more code than this – coderGeek Jan 30 at 6:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.