How to load function before state load?

Hello im trying to load function before app load main view any idea how to do this?

function($http) {
       $http.get('http://echo.jsontest.com/key/value/one/two').then(function(resp) {
            app.ConfigJSON = resp;
          }, function(err) {
           console.log(err);
          };
        }
    }

The magic world is “resolve”

The ui-router module for ionic state-definition provides resolves, where you can define code blocks, which have to run before changing to the state:

The response of the functions (code-blocks) are injected in the controller of the state


but this would call the resolve everytime you change to that state.

so i would create a service where you store your appconfig and if it is not there -> you load it if not you will send the request.

In my apps i have an own request-service. it is a wrapper for $http to handle errors, success, authorizations and so on in a single point. There i check, if i want to send a request - is the appconfig already loaded -> if not i send the config-request before and then the original request.

1 Like