Free Angular course with real-time projects Start Now!!
Since we had studied, AngularJS Modules. Today, we will talk about AngularJS MVC. MVC stands for Model View Controller, it is a software design pattern for designing and developing a web application. Here, we will learn MVC architecture & components and How MVC works in Angular JS.
So, are you ready to explore the Angular Model View Controller (MVC)?
1. What is AngularJS MVC?
MVC is nothing but the software design pattern for designing and developing a web application. It is considered as the architectural pattern that has existed for a long time in software engineering. Many languages use MVC architecture with little variations in each one of them but conceptually it remains the same.
In angular, we organize things in some different manner as compare to regular javascript. Therefore, Model View Controller architecture is the most popular and latest way of organizing an application.
Do you know What are Directives in AngularJS?
2. AngularJs MVC Architecture & Components
AngularJS MVC divides an application into three parts- Model part, View Part, Controller Part. Let’s see for what purpose each component of MVC serves:
- MODEL PART
Model part consists of a database (use to store data), application data (application data are variables that are specific to a user. Application and logic (Logic refers to code that is written to perform a certain task).
- VIEW PART
View part is the user interface part of an application. The information that is being displayed to the user in an application is written in this part.
- CONTROLLER PART
Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!
The interaction between the model part and view part is controlled by a controller. It is more like software code.
3. How MVC Works in AngularJS?
We can implement MVC pattern through JavaScript and HTML. HTML helps to implement model part, while JavaScript is for model and controller part.
Recommended reading – Data Binding in AngularJS
When a user enters a URL in the browser, according to that specific URL control goes to the server and calls the controller. After that controller uses the appropriate model and appropriate view so that appropriate response can be made for user’s request. It creates the response and that response is sent back to the user.
4. MVC In AngularJS
Here, we will see how the components of MVC works in AngularJS:
i. Model Part In Angular Application
It responds to the request made from view part. It can obtain data dynamically means you can get data from database like from MySQL or you can get data from static JSON (JavaScript Object Notation) file.
<script> $scope.person = { 'Name' : 'Ram Kumar ', 'Address' : 'AB Road, Katni', } </script>
ii. View Part In Angular Application
It is the presentation part of an application. Through the view part, a user can see the Model part. Therefore, the model and view part join together.
<h1> {{person.name}} </h1>
Also, read – ngmodel in AngularJS
iii. Controller Part In Angular Application
It is the central part of the angular application. Everything starts with a controller in angular. The model part and the view part join with each other. It means whatever happen to the model part automatically effects to the view part and whatever happens to the view part automatically effects to the model part also. Controller joins the model and view part.
function address($scope) { // Model Part is written here }
Look at this final code with a model part, view part and controller part in the single code
<!doctype html> <html ng-app> <head> <title>Angular MVC </title> <script src=”lib/Angular/angular.min.js”></script> </head> <body> <div ng-controller = “address”> <h1>{ {Person.Name} }</h1> </div> <script type=”text/javascript”> function address($scope){ $scope.Person= { 'Name' : 'Ram Kumar ', 'Address' : 'AB Road, Katni', } } </script> </body> </html>
Output:
Ram Kumar
Have a look – Features of AngularJS
5. Conclusion
This is how MVC architecture works in angular. The various aspects of an application divided into three components (Model, View and Controller) to separate responsibilities. A model contains application data, a view contains the visual layout and a controller connects the view part and model part. Furthermore, if you have any query, feel free to ask in the comment box.