1

I am loading data (idle time and timeout time) from the database via rest api call. From frontend side I am using AngularJs 1.3.1 $http get call to get the data.

I need the above data (idle time and timeout time) at the app.config() level --- which is working fine.

I need to pass the same data (idle time and timeout time) from app.config() to app.run(). Any idea how to do that?

Also how to make sure $http get call is completed and idle time and timeout time is fetched from the database before idle time and timeout time is sent to app.run()?

I hope people will understand the question and respond to it. I am stuck at it right now.

code block:

angular.module().config(function() {
    var idleWarnTime, idleTimeOut;
    var http = angular.injector([ 'ng' ]).get('$http');
    
    http.get('timeout').success(function(response) {
        idleWarnTime = response.data.warntime;
        idleTimeOut = response.data.timeout;
        
    }).error(function(error) {
        console.log('session timeout details fetching from db failed');
    });
});


angular.module().run(function() {
    //need idleWarnTime and idleTimeOut values here and only after "timeout" rest api provide the result
});
georgeawg
  • 48,608
  • 13
  • 72
  • 95
  • 2
    The `.success` and `.error` methods are deprecated and have [been removed from the AngularJS framework](https://stackoverflow.com/questions/35329384/why-are-angularjs-http-success-error-methods-deprecated-removed-from-v1-6/35331339#35331339). – georgeawg Feb 13 '21 at 04:21
  • Thanks for the information, I will change the code accordingly. Still do you know the solution of the above problem statement i.e. passing variable from app.config() to app.run()? – dexterous-unwrapped Feb 13 '21 at 04:23
  • The AngularJS framework was not designed to wait for responses from a server before starting its **run phase**. Delaying the **run phase** is likely to leave the framework unresponsive during that delay. This could make your app unpleasant for the user. It would be better find a different approach to what you are trying to do. – georgeawg Feb 13 '21 at 17:47
  • Can i use async and await in this scenario? – dexterous-unwrapped Feb 13 '21 at 17:49
  • 2
    JavaScript `async` and `await` code uses ES6 promises which are not integrated with the AngularJS framework and its digest cycle. AngularJS modifies the normal JavaScript flow by providing its own event processing loop. This splits the JavaScript into classical and AngularJS execution context. Only operations which are applied in the AngularJS execution context will benefit from AngularJS data-binding, exception handling, property watching, etc. – georgeawg Feb 13 '21 at 17:56
  • Can you just inject the `$http` service into the `.run` function and work with that? Why do you need to do XHR operations during the **config phase** of the AngularJS bootup? – georgeawg Feb 13 '21 at 18:00
  • The module which i am working on is session timeout using ng-idle which is internally using Idleprovider. Idleprovider is injected at the config function and idle warning time is getting set. This idle warning time is used at app.run() to display dialog box if user is idle for that much time. Please suggest an alternate approach if possible. Can i inject providers at app.run()? – dexterous-unwrapped Feb 13 '21 at 18:39
  • Http get call is required to get the idle warning time from the db which will be different for different environments. For example, dev - idle warning time will be 20mins, stage - 15 minutes and production - 10 minutes. And i need to set this idle warning time using Idleprovider only. Is there a way to get environment specific value from property file in angular at app.config(), if we want to avoid http call in config? – dexterous-unwrapped Feb 13 '21 at 18:58
  • 1
    Have you checked the [documentation for the `Idle` service](https://github.com/moribvndvs/ng-idle/wiki/Idle-and-IdleProvider#idle) of [ng-idle](https://github.com/moribvndvs/ng-idle)? It provides a method for changing the Idle time which can be used in a run block or a controller. – georgeawg Feb 14 '21 at 20:26
  • Hi @georgeawg, yes I have coded my module after going through the complete documentation of ng-idle. But yes, I have bypassed the fact that idle time and timeout time can be configured at the run block as well. I tried doing the way as suggested by you and it worked. Thanks for that. So the bottom line is we can not inject providers in run block. Injecting $http and calling api in angular config block will not help much as the http call is asynchronous and will not get the response before calling chain goes to run block. – dexterous-unwrapped Feb 16 '21 at 05:09

0 Answers0