javascript - Angular.js - running a service function before anything else? - Stack Overflow

Can you run an Angular service (or a function on that service) before anything else? Ideally, as soon a

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.

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.

Share Improve this question asked Sep 12, 2014 at 9:14 NekkoruNekkoru 1,1653 gold badges12 silver badges19 bronze badges 4
  • you need to create a new injector, angular.injector() – noypi 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. – noypi 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? – Dreamwalker 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. – noypi Commented Sep 12, 2014 at 10:22
Add a ment  | 

2 Answers 2

Reset to default 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/guide/module

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);


  }]);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742370203a4431111.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信