Why It’s Easier to Succeed With Angular MVC Than You Might Think

Developing applications in JavaScript is always a challenge. Due to its malleable nature and lack of type checking, building a decent size application in JavaScript is difficult. Moreover, we use JavaScript for all types of processes such as User Interface (UI) manipulation, client-server interaction, and business processing/validations. As a result, we end up with spaghetti code that is difficult to maintain and test.

Libraries such as jQuery do a great job of taking care of various browser quirks and providing constructs that can lead to an overall reduction in lines of code. However, these libraries lack any structural guidance that can help us when the codebase grows.

This is where architectural patterns such as Model View Controller (MVC) and frameworks such as AngularJS come into the picture. This chapter is dedicated to understanding the concept behind MVC architecture and learning how to put these principles into practice while we build a simple app using AngularJS and learn more from Angular Course

The basics of Model View Controller

MVC is a UI architectural pattern that has been around for some time. It is based on the premise of separation of responsibility. In MVC, we have the following components:

  • Model: This stores business data
  • View: This represents the model in UI
  • Controller: This is responsible for coordinating between a model and view

Any change in the model is reflected in the view and any change done by the user while interacting with the view is reflected in the model. Here, the controller acts as a coordinator and is responsible for keeping the model and view in sync.

This is an over-simplified definition of the MVC and if we search the Web, we will find numerous variations of this pattern such as MVP, Front Controller, MVVM, and maybe some others. The net effect of this separation is that the code becomes more organized, more understandable, and maintainable. To Get in-touch for more at Angularjs training 

For us, the best way to understand MVC is to see it in action and hence, we are going to build our first Hello World app in AngularJS. This app will help us to become familiar with the AngularJS framework and see the MVC paradigm in action.

The controller

The controller manages the model and view. It is always designed with the view in mind and it is the view’s behavior that drives controller functionality. In AngularJS, the controller is a JavaScript class (a constructor function) that hosts the model and exposes some behavior that the view binds to. How it binds to the view will be clear when we discuss the view implementation.

Let’s start working on the controller implementation. While defining our model, we have already detailed the functional aspect of the application and we do have a fair idea about how the view should behave. Keeping that in mind, this is how the app controller looks:

function GuessTheNumberController($scope) {
$scope.verifyGuess = function () {
$scope.deviation = $scope.original – $scope.guess;
$scope.noOfTries = $scope.noOfTries + 1;
$scope.initializeGame = function () {
$scope.noOfTries = 0;
$scope.original = Math.floor((Math.random() * 1000) + 1);
$scope.guess = null;
$scope.deviation = null;

Add this controller script to the file created earlier after the Angular script declaration inside its script block.

The GuessTheNumberController function sets up some model properties that we described in The app model section and exposes two methods: verifyGuess and initialize the game.

The verifyGuess function verifies whether the guess matches the original value and updates model properties deviation and notaries accordingly. The initializeGame function is used to initialize the model properties before the start of the game, and during the game whenever the user clicks on the Restart button. To get in-depth knowledge about Angular MVC to follow my page  Angular Js Online Training 

The last statement in the preceding controller calls initializeGame to set up the game for the first time.

The overall controller implementation is self-explanatory but the only oddity seems to be the $scope object. This $scope object has been passed as a parameter to the controller function and all functions and properties are attached to $scope. To understand the role of the $scope object and how things tie together, we need to start implementing the view.

However, we are still not done with the controller yet. We will revisit the controller once we get the app running and learn a bit more about them.

Comments are closed