Angularjs MVC tutorial

AngularJS MVC:

AngularJS mvc framework provides the facility to build flexible and loosely coupled applications. MVC stands for Model-View-Controller design pattern which separates the business logic, presentation logic and navigation logic.


Model: is responsible for encapsulating the application data.
View: is responsible for rendering the model data.
Controller: is responsible for receiving the user request, building model object and passing the model object to the view.

Example Explanation:

First include the AngularJS library in the application. The ng-app directive initializes the application. The ng-model directive binds the state of the input text to the respective name variables (student.firstName and student.lastname). The ng-controller=”appController” directive defines the controller. The appController defined as a JavaScript object with $scope as argument. The $scope.student is property of appController object. In $scope.student.fullName we are getting the combined name (firstName + lastName).
Note: We can also store controllers in external files .JS file and refer that file in the html page.


    <title>Angular MVC Example</title>
    <script src = 
      <h2>AngularJS MVC Example</h2>
      <div ng-app = "testApp" ng-controller = "appController">
         Enter first name: 
         <input type = "text" ng-model = "student.firstName">
         Enter last name: 
         <input type = "text" ng-model = "student.lastName">
        Entered Value: {{student.fullName()}}
         var mainApp = angular.module("testApp", []);
         mainApp.controller('appController', function($scope) {
            $scope.student = {
               firstName: "Vivek",
               lastName: "Solenki",
               fullName: function() {
                  var studentObject;
                  studentObject = $scope.student;
                  return studentObject.firstName + " "
                                + studentObject.lastName;

Try it:

Angular MVC Example on

Comments are closed.