Difficulty: Medium
Correct Answer: The browser loads the HTML page and AngularJS script, Angular scans the DOM for ng app or manual bootstrap calls, creates a root injector, compiles templates with directives, and links them to scopes and controllers to start the application.
Explanation:
Introduction / Context:
The boot or bootstrap process in AngularJS describes how the framework starts an application when a page is loaded. Understanding this flow helps developers reason about when directives are compiled, when controllers are instantiated, and how dependency injection is set up. Interview questions on this topic check awareness of how AngularJS initializes itself in the browser.
Given Data / Assumptions:
Concept / Approach:
When the browser finishes loading the HTML and JavaScript, AngularJS looks for the ng app directive, which marks the root of the AngularJS application. It then creates a root injector responsible for providing services and other dependencies. Next, AngularJS compiles the DOM by walking through elements and processing directives. This compilation phase turns directives into behaviour and prepares link functions. After that, the framework links the compiled templates to specific scopes and controllers. At this point, data binding is active, and watch expressions are set up so that changes in models update the view and user interactions can update the models.
Step-by-Step Solution:
Verification / Alternative check:
AngularJS documentation describes two phases, compile and link, in its digest cycle. Bootstrapping is shown with examples using ng app on the html element or a specific div. When the page loads, AngularJS runs automatically and attaches to the specified element. Manual bootstrap examples call angular.bootstrap with a root element and a list of modules, demonstrating that AngularJS explicitly sets up the injector and compiles the DOM at startup.
Why Other Options Are Wrong:
Common Pitfalls:
One common issue is placing multiple ng app directives in nested containers without understanding that each creates a separate AngularJS application. Another pitfall is trying to interact with AngularJS before it has finished bootstrapping, which can lead to undefined services or controllers. Properly structuring modules and letting AngularJS complete its startup process simplifies application behaviour.
Final Answer:
The correct choice is The browser loads the HTML page and AngularJS script, Angular scans the DOM for ng app or manual bootstrap calls, creates a root injector, compiles templates with directives, and links them to scopes and controllers to start the application. because this describes the essential steps of the AngularJS boot process in the correct order.
Discussion & Comments