Angular JS Views

AngularJS supports Single Page Application. ng-view tag,(html or ng-template view) can be added to a HTML Single view application.

by Paola D'Antonio

HTML

<html>
   
   <head>
      <title>Angular JS Views</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
      <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-route.min.js"></script>
   </head>
   
   <body>
      <h2>Angular JS Views</h2>
      <div ng-app = "mainApp">
         <p><a href = "#addStudent">Add Student</a></p>
         <p><a href = "#viewStudents">View Students</a></p>
         <div ng-view></div>
         
         <script type = "text/ng-template" id = "addStudent.htm">
            <div id="addStudentStyle">
            <h2> Add Student </h2>
            {{message}},
            {{description}}
            </div>
         </script>
         
         <script type = "text/ng-template" id = "viewStudents.htm">
          <div id="viewStudentStyle">
            <h2> View Students </h2>
            {{message}},
            {{description}}
            </div>
         </script>
      </div>
      
            
   </body>
</html>

CSS

#viewStudentStyle{
    background-color: lightblue;
    }
 
#addStudentStyle {
  background-color: lightpink;
}

TypeScript

var mainApp = angular.module("mainApp", ['ngRoute']);
         mainApp.config(['$routeProvider', function($routeProvider) {
            $routeProvider.
            
            when('/addStudent', {
               templateUrl: 'addStudent.htm',
               controller: 'AddStudentController'
            }).
            
            when('/viewStudents', {
               templateUrl: 'viewStudents.htm',
               controller: 'ViewStudentsController'
            }).
            
            otherwise({
               redirectTo: '/addStudent'
            });
         }]);
         
         mainApp.controller('AddStudentController', function($scope) {
            $scope.message = "This page will be used to display add student form";
            $scope.description = "Add all the students you need";
         });
         
         mainApp.controller('ViewStudentsController', function($scope) {
            $scope.message = "This page will be used to display all the students";
             $scope.description = "View all the students you need";
         });