What is the calling order of angularjs functions (config/run/controller)? [duplicate]
Asked Answered
E

1

6

There are

  • controllers
  • constants
  • directives
  • services
  • factory
  • run
  • config
  • filters

functions of angular.js. What is the calling order of all these modules?

Elm answered 14/7, 2016 at 12:25 Comment(2)
check this example fiddle.jshell.net/afluegge/apo91559/lightSnowmobile
Put 3 console.logs and find out yourself?Lauro
I
16

Learning this I made a fiddle observing the behaviour by console.log. Its like

  • app config
  • app run
  • directive setup
  • directive compile
  • (app controller dependencies)
    • service
    • factory
    • inner factory
    • inner service
  • app controller
  • filter
  • directive linking
  • filter render (w.r.t the markup)

Observe yourself here (Check Console).

EDIT

New Fiddle with filters added

Impassable answered 14/7, 2016 at 12:32 Comment(10)
and filters place?Elm
@SunilGarg Please check the updated answer. I've added a filter too.Impassable
thanks! but if i am injecting filter in the controller then the filter will get loaded first. Same with the service, if inject service in run then this will get loaded before run method.Elm
Yes basically after directive compile the controllers dependencies are loaded and then the services factories filters get loaded w.r.t the dependencies which are injected in the controller.Impassable
Will this order be maintained no matter the order in wich the programmer calls these functions ?Emancipation
where does components come in this order ? Angular Js 1.5 has components !Gorse
Just to add: App Constant Providers, App Value Providers are executed in same order before App config is executedZoospore
I downvote this as tthis answer is misleading... the documentation states clearly that provider functions are executed first (Value, Factory, Service, Provider, Constant) and then config and finally run blocks. You may notice that inside config or run blocks you can inject a service... that would be possible if config or run blocks are evaluated firsts. You may refer to docs.angularjs.org/guide/…Sulphurize
@Sulphurize this is really an old answer. and i posted this answer while i was learning angularjs. I just demonstrated the behavior of angularjs with console.logs as you see above with fiddles. but maybe this is changed now but previous it worked like thatImpassable
@M.JunaidSalaat older or not people like me still find this and as long this content is reachable I guess you may amend it. Not my intention to be hard or anything, just trying to do a right thing. Greetings.Sulphurize

© 2022 - 2024 — McMap. All rights reserved.