티스토리 뷰

Today, we will explore AngularJS, a JavaScript MVC framework, and analyze its development pattern, the MVC (Model-View-Controller) structure. AngularJS has evolved since its inception, so let's take a look at its updated features and advantages.


Introduction to AngularJS

AngularJS is an opensource JavaScript framework that extends HTML for dynamic web application development. It provides a robust set of features and follows the MVC architectural pattern. With AngularJS, developers can build responsive and interactive web applications efficiently. Some key features of AngularJS include:

1. Two-way data binding: AngularJS simplifies data synchronization between the model and view by automatically updating the view when the model changes, and vice versa.

2. Directives: AngularJS introduces the concept of directives, which allows developers to extend HTML with custom attributes and elements. Directives enable the creation of reusable components and enhance the declarative nature of HTML.

3. SPA (Single Page Application) support: AngularJS facilitates the development of SPAs by offering features like routing, which enables seamless navigation between different views without page reloads.

4. Dependency injection: AngularJS provides a built-in dependency injection mechanism, allowing developers to manage and inject dependencies into components, promoting modularity and testability.

5. Templating: AngularJS uses HTML as a template language, making it easier to create dynamic views and declaratively bind data to the view.

While Angular and other frameworks like React and Vue.js have gained popularity in recent years, AngularJS still remains in many legacy codebases. It's important to note that AngularJS and Angular (also known as Angular 2+) are distinct frameworks with different features and syntax.


AngularJS MVC Structure and Execution Principles

The MVC architecture plays a central role in AngularJS application development.

1. Model (M): The model represents the data and business logic of the application. In AngularJS, the model is often defined as a JSON structure, which can be accessed and manipulated within the controllers. To bind the model to the view, AngularJS uses the ng-model directive.

2. View (V): The view is responsible for rendering the user interface. It is typically created using HTML templates and enhanced with AngularJS directives and expressions. The view interacts with the model through data binding, displaying and updating the data in response to user actions.

3. Controller (C): Controllers handle the application's logic and act as an intermediary between the model and the view. They retrieve data from the model, perform necessary operations, and update the model accordingly. In AngularJS, the $scope service acts as the bridge between the controller and the view, allowing easy access and manipulation of data. Controllers are defined using the ng-controller directive.

The AngularJS execution model allows real-time updates and efficient data synchronization between the model and the view. Unlike traditional web applications, which require manual updates, AngularJS automatically reflects changes in the view as soon as the underlying data changes.

In addition to the MVC structure, AngularJS provides routing capabilities. Similar to back-end routing in frameworks like Node.js, AngularJS enables front-end routing, allowing efficient navigation between views based on user interactions. 


AngularJS MVC Code Structure 

Let's take a look at the basic code structure of an AngularJS application.


This represents the fundamental structure of an AngularJS application. The structure can evolve and become more complex over time. In this post, I will focus on explaining how the MVC structure is translated into code. The code structure of AngularJS follows a hierarchical tree-like organization. The top-level attribute is ng-app, which refers to the AngularJS module in the JavaScript part of the application. In most AngularJS projects,

 the ng-app attribute is typically set on the <html> tag, indicating that the HTML file follows the AngularJS model. Controllers are implemented within <div> elements, allowing the grouping of related functionality within a controller code block. To define a controller, a callback function called controller() is used. Multiple controllers can be created based on different functional areas. Finally, the binding between the controller and the view needs to be established. The $scope interface facilitates this binding, allowing the view to access the data (model) within the controller using $scope.modelName notation. When values are updated, the view is automatically refreshed. Each scope can access the data within its corresponding controller area.

In conclusion, we have explored the MVC structure of AngularJS. While other frameworks like Angular and React are more commonly used today, AngularJS still exists in many legacy codebases. In the next post, we will explore the directives and {{expressions}}.