AngularJS Documentation of .run() function - angularjs

I am working with angularJS routing. In this regard I need documentation of .run() function.What is the responsibility of .run() function.Thanks

From the docs
Run Blocks
Run blocks are the closest thing in Angular to the main method. A run
block is the code which needs to run to kickstart the application. It
is executed after all of the services have been configured and the
injector has been created. Run blocks typically contain code which is
hard to unit-test, and for this reason should be declared in isolated
modules, so that they can be ignored in the unit-tests.
run() is also the earliest you will have access to $rootScope. It is a good place to register states and routes.

Related

How do I suppress unwanted ui-router 'otherwise' transitions in my unit tests?

Using the latest #ui-router/angularjs, I have a state configured with an otherwise block:
$urlRouterProvider.otherwise('/page');
I also have a .run() block that uses a $transitions.onBefore() hook to perform a security check, similar to ui-router's sample app:
My problem is that any of my Karma tests that run trigger the following:
The .otherwise() block runs
Which in turn tries to navigate to /page
Which causes the .run() block to run
Which performs $http service calls, among other unnecessary work
This happens for any test that runs angular.mock.module('module'), even if I'm not explicitly testing any routing behavior. Even if I'm just loading $componentController and nothing else.
I could get around this by adding $httpBackend mocks for the service calls happening in my .run() block, but I feel like there should be a better way.
So, what's the best practice for testing once you have default routing and .run() blocks as ui-router recommends?

AngularJS: how angularjs executes?

I'm sure this is boring - it has bothered me for some time now.
I wonder how AngularJS executes?
AngularJS provides some basics Module's Service, Provider and Factory and functions like app.run() and app.config().
What is the executing order of these modules and functions?
If I want to execute a function before the controller or directive executes where should I place it?
AngularJS first gathers everything. Maybe you have written custom directives and filters and external components, AngularJS will first gather each and every resource. Then, it would try to satisfy all the mentioned and required dependencies. So, if your module depends on any external module then, angular would first, load the external module and then would pass it to dependent module.
Now, for app.run() and app.config () methods.
A function passed to app.run() will be executed when all the modules have been loaded. It means all the modules, including external ones.
And a function passed to app.config() will be executed when current module have been loaded.
If I were you, I would place the redirect code in config() method.

.config, .run, AppCtrl - where to put routes?

I wanted to find out the difference between the .config and .run functions in AngularJS. I was using my .config for setting up routes, but I did have some $on's for watching route change start and success events.
I then moved some of this code to .run as I was having some dependency injection problems in .config.
I finally moved some of this to a CommonAppController which I have set on my <body>.
I also had 2 .config's and it seemed to be running ok, but surely this isn't right?
Can anyone give a little insight on which method to use?
Configuration blocks and run blocks get executed at different points in the application bootstrap, and have different injection locals at their disposal. Here's a summary of what you can find in the AngularJS documentation.
Configuration blocks (registered with module.config()) get executed during provider registration, and can only be injected providers and constants (see module.provider() and module.constant()). This is typically where you would configure application-wide stuff, such as the $routeProvider. Stuff that needs to be configured before the services are created.
Run blocks (registered with module.run()) get executed after the injector has all the providers. Now, all instances and constants can be injected. This is typically where you would configure services, $rootScope, events and so on.
You can have multiple of either, and they are executed in the order they were registered to the module. Some people prefer to register a configuration block before every group of controllers to register the routes to these controller, for example.
The .config block is executed during the provider registration and configuration phase. It' a module level block.
The.run block is executed after the config block. It's used to inject services and constants.

what can or cannot be done with a controller created in a module Vs controller created globally?

Have seen various angular JS examples and I get the hint that I should define controllers on a module rather than a global definition.
Say I have an app that has <body ng-app='mymodule'> - inside body it can acknowledge controllers created in mymodule and controllers created globally.
The controller defined on the module will not have access to any global
variables as compared to the global controller. True/False ?
Any other important points to note here ?
The controller defined on the module will not have access to any global variables as compared to the global controller. True ?
False. Global variables are, well, global. You can access them from within the controller, but you shouldn't, unless testing isn't a concern for you - and it definitely should be.
Any other important points to note here ?
A global controller cannot use services from other modules but from Angular itself. If you want to use components defined in another modules (directives, factories, providers, etc.) you'll have to create your own module and declare them as dependencies so you can inject the bits you need wherever you need them.
The only use for a global controller, IMO, is to demonstrate Angular in a simple, easy-to-understand way. Don't use it in production.
Update
As #dotnetcoder pointed out in the comments, apparently due to the way Angular loads modules, a global controller can actually access the services from a module if that module was already loaded by the framework. I stand corrected. But, although technically possible, I can't think of any reason anyone should do that and I stand by what I've said previously: don't use global controllers in production.

Angularjs suitable moment to launch a http request when app start

I just started to learn Angularjs 2 days ago. A question have confused me for all the 2 days.
I need to make a http request to the server to get some data when the app start, but I cannot find the suitable moment to do this.
I've tried to make a controller, which calls $http.get(). But It doesn't work. It seems that the controller won't be instantiated if it's never used in the html (not sure about this).
I also tried to find other ways, but I only found $http which is used for http request. And $http only appears in the controller.
Maybe I need use other Angularjs methods? Or, I should do the instantiate action manually?
Something like:
angular.module('yourApp').run(['$rootScope', '$http', function ($rootScope, $http) {
// do stuff here
}]);
From the documentation:
Run Blocks
Run blocks are the closest thing in Angular to the main method. A run block is the code which needs to run to kickstart the application. It is executed after all of the service have been configured and the injector has been created. Run blocks typically contain code which is hard to unit-test, and for this reason should be declared in isolated modules, so that they can be ignored in the unit-tests.

Resources