Execute method only once in Angularjs

I have a scenario where I function

only want to run once on my site loaded (to do some security checks). But I don't know how to achieve this with Angular.

What I have tried:

.run //(not sure about this)

angular.element(document).ready

      

Please guide you through this with Angular.

+3


source to share


1 answer


I think your first guess ( .run

) was the closest. The section "Loading and Dependencies of Modules" talks about configuration blocks and run blocks:

A module is a collection of configuration and runtime blocks that are applied to an application during the boot process. In its simplest form, a module consists of a set of two types of blocks:

  • Configuration blocks - Executed during vendor registration and configuration. Only providers and constants can be entered in configuration blocks. This is to prevent accidental creation of service instances before they are fully configured.
  • Run Blocks - execute execution after the injector is created and use it to launch the application. Only instances and constants can be entered into run blocks. This is to prevent further configuration of the system while the application is running.


angular.module('myModule', []).
config(function(injectables) { // provider-injector
  // This is an example of config block.
  // You can have as many of these as you want.
  // You can only inject Providers (not instances)
  // into config blocks.
}).
run(function(injectables) { // instance-injector
  // This is an example of a run block.
  // You can have as many of these as you want.
  // You can only inject instances (not Providers)
  // into run blocks
});

      

+3


source







All Articles