Skip to main content
Announcements
Have questions about Qlik Connect? Join us live on April 10th, at 11 AM ET: SIGN UP NOW
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Sense - AngularJS - $scope not populating correctly?

Hi everyone,

I have an extension, and in my template, I have the following code:

<button ng-click="getApps()">Get apps</button>

<div ng-repeat="a in allApps" style="clear:left">

  {{a.qTitle}}

</div>

In the extension's javascript code, I have the following code fragment:

controller: ['$scope', 'testService',

                function($scope, testService) {

                    $scope.getApps = function() {

                        testService.getApps().then(function(reply) {

                            $scope.allApps = reply;

                        });

                    };

     // rest of the code...

The testService code has the following definition for getApps():

getApps: function() {

                    var deferred = q.defer();

                    c.getAppList(function(reply) {

                        deferred.resolve(reply);

                    });

                    return deferred.promise;

                },

-----

When I click the 'Get Apps' button in my view, everything works as expected - I see a list of apps (their qTitle) in the view... GREAT!

However, and this has been driving me crazy, I cannot get the code to work when the app starts up... I want to see it when the extension runs for the first time... I can get it when I click the button, but I need it there 'straight away' (I know it's an async call, that's why that's in quotes).  I don't see any errors (in the dev tools), but I don't get anything back either.

Any help would be much appreciated...

THANKS!

Shane.

1 Solution

Accepted Solutions
Stefan_Walther
Employee
Employee

Hi,

some thoughts:

  • try wrapping it in a $timeout ... sometimes this helps to find out if something hasen't been loaded properly (fast enough at the beginning); if this is the case, double-check your dependencies ...

Regards

Stefan

View solution in original post

5 Replies
Michael_Tarallo
Employee
Employee

Hi Shane - I have informed a few members who may be able to help you.

Mike

Regards,
Mike Tarallo
Qlik
Brian_Munz
Employee
Employee

Hi Shane,

If I understand your question, I think you might just be able to just run the function in your JS as a sort of init.  So you could simply do $scope.getApps()...

Or create an init function to run when the page loads, and put the same function in there.

$scope.init = function(){

  //do whatever you need to do to get things rolling

$scope.getApps()

}

Does that make sense?

Not applicable
Author

That's what I thought too.  Seems like a very logical thing to do

However, it doesn't seem to work at all.  The data isn't returned.  I can write code (let's say a console.log(...) call, and that works, but when I call the promise returning function, the scope isn't updated.

Stefan_Walther
Employee
Employee

Hi,

some thoughts:

  • try wrapping it in a $timeout ... sometimes this helps to find out if something hasen't been loaded properly (fast enough at the beginning); if this is the case, double-check your dependencies ...

Regards

Stefan

Not applicable
Author

Hi Stefan,

that was it! I wrapped the code in a $timeout as you suggested, and I now get something!

I was under the impression, however, that requirejs ensured that script (dependencies) were loaded before things were returned. 

Thanks again!

Shane.