First, the start-up phase
As you all know, when a browser loads an HTML page, it parses the HMTL page into a DOM tree and loads each element node in the DOM tree one by one. We can think of Angularjs as a jquery-like JS Library, we introduce the <script> tag into the HTML, then angular as a normal DOM node waiting for the browser to parse, when the browser resolves to this node, Found that it is a JS file, then the browser will stop parsing the remaining DOM node, start to execute this JS (ie, angular.js), while angular will set an event listener to listen to the browser domcontentloaded event. When angular hears this event, it launches the angular application.
Ii. phase of initialization
When angular starts, it looks for the Ng-app directive, initializes a series of necessary components ( i.e. $injector, $compile service, and $rootscope), and then starts parsing the DOM tree again.
Third, compile, link
$compile service looks for DOM elements with declarative instructions by traversing the DOM tree. When a DOM element with one or more instructions is encountered , it sorts the instructions (based on the priority precedence of the instruction) and then uses the $injector service to find and collect the compile function of the instruction and execute it.
after each node's compilation method runs, the $compile service calls the link function. This link function sets the monitoring for the command that binds the closed scope. This behavior creates a live view.
Finally, when the $compile service is complete, the ANGULARJS runtime is ready.
iv. operation phase
the angular provides its own event loop. The instruction itself will note The event listener, so when the event is triggered, the instruction function runs in the Angularjs $digest loop. $digest Loop waits for the $watch expression list, and when the model changes are detected, the $watch function is called, and the $watch list is reviewed again to ensure that no model is changed.
Once the $digest cycle stabilizes, and no potential changes are detected, the execution process leaves the angular context and usually goes back to the browser, where the DOM will be rendered.
Draw the key steps of the above process into a graph, as follows:
Angularjs Execution Process