When a page is loaded into the browser, several things happen:
- HTML document file gets loaded, and evaluated by the browser. AngularJS JavaScript file gets loaded, and the angular global object is created. Next, JavaScript file which is responsible for registering the controller functions is executed.
- AngularJS scans through the HTML to find AngularJS apps and views. Once the view is found, it connects that particular view to the corresponding controller function.
- AngularJS executes the controller functions. It further renders the views with data from the model populated by the controller, and the page gets ready.
- AngularJS has a specific bootstrapping process that initializes and starts the framework. Here is a general overview of the AngularJS boot process:
- HTML Document Loaded:
- AngularJS applications typically begin when the HTML document is loaded in the browser.
ng-app
Directive:- The
ng-app
directive is used to define the root element of the AngularJS application. It marks the starting point for AngularJS to take control.
- The
- Module Initialization:
- AngularJS applications are modular, and they are organized into modules. The
angular.module
function is used to create a module. During bootstrapping, AngularJS identifies the module(s) defined using theng-app
directive and initializes them.
- AngularJS applications are modular, and they are organized into modules. The
- Dependency Injection:
- AngularJS uses dependency injection to manage components and their dependencies. Controllers, services, and other components are instantiated and injected with their dependencies based on the module configuration.
- Compile Phase:
- AngularJS traverses the DOM, looking for elements containing AngularJS directives (e.g.,
ng-controller
,ng-model
). It then compiles these directives, linking them to the corresponding AngularJS controllers and creating a connection between the view and the model.
- AngularJS traverses the DOM, looking for elements containing AngularJS directives (e.g.,
- Runtime Phase:
- After the compile phase, AngularJS enters the runtime phase. This is where the application starts responding to user interactions, and the two-way data binding mechanism keeps the model and the view in sync.
- Digest Loop:
- AngularJS maintains a digest loop, which is a mechanism for tracking changes to the model and updating the view accordingly. During each iteration of the digest loop, AngularJS checks for changes in the model and updates the view if necessary.
- Event Handling:
- As users interact with the application, AngularJS handles events, such as button clicks or form submissions, through the defined controllers and services.
- Routing (if applicable):
- If the application uses AngularJS routing, the router takes control and manages navigation within the application, loading different views and controllers dynamically.
Understanding the AngularJS boot process is crucial for developers to effectively build and maintain AngularJS applications. Keep in mind that AngularJS is an older framework, and the latest version of Angular is Angular (2+), which has a significantly different architecture
- HTML Document Loaded: