4

Can you run an Angular service (or a function on that service) before anything else? Ideally, as soon as ng-app gets parsed.

Here's my use case: I'm writing an app that gets AJAX data from a server and then parses the data a hundred different ways. I would like to make the initial AJAX call before all the controllers get called? That way I just have all the data parsed and loaded in the service without me worrying about updating any controllers or whatever.

4
  • you need to create a new injector, angular.injector() Commented Sep 12, 2014 at 9:19
  • but after ng-app, that would be a different injector -- two different apps now if you initially create an injector. Commented Sep 12, 2014 at 9:20
  • Are you expecting this data to be ready as well, as it's an async call in angular? Commented Sep 12, 2014 at 9:31
  • if you already have the data before the ngapp is parsed, then you can use providers to configure your services, see my example below. Commented Sep 12, 2014 at 10:22

2 Answers 2

6

I would like to make the initial AJAX call before all the controllers get called

In Angular method run is fired before any controller is called

var app = angular.module('myApp',[]);

app.run(function($rootScope){
  // ajax call and other stuff
}

In run method you can do any job like login to Facebook, token validation and so on


Reference

Configuration blocks (aka app.config) - get executed during the provider registrations and configuration phase. Only providers and constants can be injected into configuration blocks. This is to prevent accidental instantiation of services before they have been fully configured.

Run blocks (aka app.run) - get executed after the injector is created and are used to kickstart the application. Only instances and constants can be injected into run blocks. This is to prevent further system configuration during application run time.

docs.angularjs.org/guide/module

Sign up to request clarification or add additional context in comments.

5 Comments

Shouldn't it be on the config method? Since he wants to config the service to contain all the data?
@JoaoLeal right, SO didnt talk about, i edit my answer
So in theory we would use config to config the service and then run to actually call the AJAX methods to pull the data from the server, right?
@JoaoLeal not at all. if you want to config service, use provider instead service or factory. here is good demo: jsfiddle.net/pkozlowski_opensource/PxdSP/14
Run method will not work if you want the ajax call to be completed before anything else...
0

plnkr = http://plnkr.co/edit/WTNuWKSgj0bMR1dtUkto?p=preview

The best way to configure how your services behave is to use providers. so, assuming you already have a mydata from your ajax call, the plnkr above shows a running example...

  myapp.config(['sayHelloProvider',function(sayHelloProvider){

    // assuming your ajax retrievies mydata
    var mydata = angular.fromJson(  angular.element(document.getElementById('mydata')).html() );

    // configure service
    sayHelloProvider.SetMessage("Olah! rate is=" + mydata.rate);


  }]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.