How to create Single Page Application via multiple views

Last updated on May 31 2022
Santosh Prakash

Table of Contents

How to create Single Page Application via multiple views

AngularJS supports Single Page Application via multiple views on a single page. To do this AngularJS has provided ng-view and ng-template directives and $routeProvider services.

ng-view

ng-view tag simply creates a place holder where a corresponding view (html or ng-template view) can be placed based on the configuration.

Usage

Define a div with ng-view within the main module.

<div ng-app = “mainApp”>

<div ng-view></div>

 

</div>

ng-template

ng-template directive is used to create an html view using script tag. It contains “id” attribute which is used by $routeProvider to map a view with a controller.

Usage

Define a script block with type as ng-template within the main module.

<div ng-app = “mainApp”>

 

<script type = “text/ng-template” id = “addStudent.htm”>

<h2> Add Student </h2>

{{message}}

</script>

 

</div>

$routeProvider

$routeProvider is the key service which set the configuration of urls, map them with the corresponding html page or ng-template, and attach a controller with the same.

Usage

Define a script block with main module and set the routing configuration.

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’

});

 

}]);

Following are the important points to be considered in above example.

  • $routeProvider is defined as a function under config of mainApp module using key as ‘$routeProvider’.
  • $routeProvider.when defines a url “/addStudent” which then is mapped to “addStudent.htm”. addStudent.htm should be present in the same path as main html page.If htm page is not defined then ng-template to be used with id=”addStudent.htm”. We’ve used ng-template.
  • “otherwise” is used to set the default view.
  • “controller” is used to set the corresponding controller for the view.

Example

Following example will showcase all the above mentioned directives.

testAngularJS.htm

 

<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>AngularJS Sample Application</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”>

<h2> Add Student </h2>

{{message}}

</script>

 

<script type = “text/ng-template” id = “viewStudents.htm”>

<h2> View Students </h2>

{{message}}

</script>

</div>

 

<script>

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”;

});

 

mainApp.controller(‘ViewStudentsController’, function($scope) {

$scope.message = “This page will be used to display all the students”;

});

</script>

 

</body>

</html>

Result

Open textAngularJS.htm in a web browser. See the result.

AngularJS – Scopes

Scope is a special javascript object which plays the role of joining controller with the views. Scope contains the model data. In controllers, model data is accessed via $scope object.

<script>

var mainApp = angular.module(“mainApp”, []);

 

mainApp.controller(“shapeController”, function($scope) {

$scope.message = “In shape controller”;

$scope.type = “Shape”;

});

</script>

Following are the important points to be considered in above example.

  • $scope is passed as first argument to controller during its constructor definition.
  • $scope.message and $scope.type are the models which are to be used in the HTML page.
  • We’ve set values to models which will be reflected in the application module whose controller is shapeController.
  • We can define functions as well in $scope.

Scope Inheritance

Scope are controllers specific. If we defines nested controllers then child controller will inherit the scope of its parent controller.

<script>

var mainApp = angular.module(“mainApp”, []);

 

mainApp.controller(“shapeController”, function($scope) {

$scope.message = “In shape controller”;

$scope.type = “Shape”;

});

 

mainApp.controller(“circleController”, function($scope) {

$scope.message = “In circle controller”;

});

 

</script>

Following are the important points to be considered in above example.

  • We’ve set values to models in shapeController.
  • We’ve overridden message in child controller circleController. When “message” is used within module of controller circleController, the overridden message will be used.

Example

Following example will showcase all the above mentioned directives.

testAngularJS.htm

 

<html>

<head>

<title>Angular JS Forms</title>

</head>

 

<body>

<h2>AngularJS Sample Application</h2>

 

<div ng-app = “mainApp” ng-controller = “shapeController”>

<p>{{message}} <br/> {{type}} </p>

 

<div ng-controller = “circleController”>

<p>{{message}} <br/> {{type}} </p>

</div>

 

<div ng-controller = “squareController”>

<p>{{message}} <br/> {{type}} </p>

</div>

 

</div>

<script src = “https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js”>

</script>

 

<script>

var mainApp = angular.module(“mainApp”, []);

 

mainApp.controller(“shapeController”, function($scope) {

$scope.message = “In shape controller”;

$scope.type = “Shape”;

});

 

mainApp.controller(“circleController”, function($scope) {

$scope.message = “In circle controller”;

});

 

mainApp.controller(“squareController”, function($scope) {

$scope.message = “In square controller”;

$scope.type = “Square”;

});

 

</script>

 

</body>

</html>

Result

Open textAngularJS.htm in a web browser. See the result.

So, this brings us to the end of blog. This Tecklearn ‘How to create Single Page Application via multiple views’ blog helps you with commonly asked questions if you are looking out for a job in Angular JS and Front-End Development. If you wish to learn Angular JS and build a career in Front-End Development domain, then check out our interactive, Angular JS Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/angular-js-training/

Angular JS Training

About the Course

Angular JS certification training is designed for developers who are responsible for building applications using AngularJS. Through a combination of presentations and hands-on practices, participants will explore Angular comprehensively and learn to build components, directives, template-driven forms, routing, etc. for complex data-centric (enterprise) applications. This Angular online training will update your knowledge and skills with the current Angular version to lift your career.

Why Should you take Angular JS Training?

  • The average salary for “Angular Developer” ranges from approximately $92,814 per year for a Developer to $113,069 per year for a Full Stack Developer (Indeed.com).
  • YouTube, Google, Cisco, Nike, Samsung, Microsoft, Forbes, BMW and many Fortune 500 companies are using Angular as their front-end development framework to deliver an engaging user experience.
  • Angular’s powerful cross-platform feature allows developers to build progressive web applications and native mobile applications.

What you will Learn in this Course?

Introduction to Angular

  • Introduction to Angular
  • Comparison between front-end tools
  • Angular Architecture
  • Building blocks of Angular
  • Angular Installation
  • Angular CLI
  • Angular CLI commands
  • Angular Modules

Angular Components and Data Binding

  • Working of Angular Applications
  • Angular App Bootstrapping
  • Angular Components
  • Creating A Component Through Angular CLI
  • Ways to specify selectors
  • Template and styles
  • Installing bootstrap to design application
  • Data Binding
  • Types of Data Binding
  • Component Interaction using @Input and @Output decorator
  • Angular Animations

TypeScript

  • What is TypeScript
  • Need of TypeScript
  • How to install TypeScript
  • Nodemon for monitoring changes
  • Interfaces in Class, String Templates, Maps,
  • Sets and Object Restructuring

Directives and Pipes in Angular

  • Angular Directives
  • @Component Directive
  • Structural Directives
  • Attribute Directives
  • Custom Directives
  • Pipes
  • Built-in Pipes
  • Chaining pipes
  • Custom pipes
  • Pipe Transform Interface & Transform Function
  • Pure and Impure pipes

Angular Services and Dependency Injection

  • Angular service
  • Need for a service
  • Dependency Injection
  • Creating a service
  • Hierarchical Injector
  • Injecting A Service into Another Service
  • Observables
  • RxJS Library
  • Angular’s Interaction with Backend
  • Parts of an Http Request
  • HttpClient

Forms in Angular

  • Forms in Angular
  • What are their functions
  • Advantages of Forms
  • Various Types of Forms
  • What is Angular Validation and Model driven approach

Angular Routing

  • Angular Routing
  • Angular Routing benefits and features
  • Building a single page application and updating it dynamically with Angular Routing
  • What is Parameter Routing
  • Router Lifecycle Hooks and Child Routes

Authentication and Security in Angular

  • What is Authentication?
  • Authentication and authorization
  • Types of Authentication
  • Where to store tokens?
  • JSON Web Tokens (JWT)
  • Authentication in Angular application
  • Security threats in web application

Testing Angular applications

  • Testing of Angular applications
  • Deployment of Angular Test Bed for testing on Angular framework
  • Setup of various tools for testing
  • Testing services in Angular Framework
  • E2E and Document Object Model testing

Got a question for us? Please mention it in the comments section and we will get back to you.

0 responses on "How to create Single Page Application via multiple views"

Leave a Message

Your email address will not be published. Required fields are marked *