Home >Web Front-end >JS Tutorial >Introduction to the view function application of AngularJS_AngularJS
AngularJS supports single page applications via multiple views on a single page. To do this AngularJS provides ng-view and ng-template directives, as well as the $routeProvider service.
ng-view
ng-view tag simply creates a placeholder for a corresponding view (HTML or ng-template view) that can be placed according to the configuration.
Use
Define a div and ng-view in the main module.
<div ng-app="mainApp"> ... <div ng-view></div> </div>
ng-template
Theng-template directive is used to create HTML views using script tags. It contains an "id" attribute for the controller view mapped by $routeProvider.
Use
Define the type as a script block for ng-template in the main module.
<div ng-app="mainApp"> ... <script type="text/ng-template" id="addStudent.html"> <h2> Add Student </h2> {{message}} </script> </div>
$routeProvider
$routeProvider is a configuration that groups URLs, maps them to the corresponding HTML page or ng-template, and attaches a controller to the service using the same key.
Use
Define the type as a script block for ng-template in the main module.
<div ng-app="mainApp"> ... <script type="text/ng-template" id="addStudent.html"> <h2> Add Student </h2> {{message}} </script> </div>
Use
Define the script block of the main module and set the routing configuration.
var mainApp = angular.module("mainApp", ['ngRoute']); mainApp.config(['$routeProvider', function($routeProvider) { $routeProvider. when('/addStudent', { templateUrl: 'addStudent.html', controller: 'AddStudentController' }). when('/viewStudents', { templateUrl: 'viewStudents.html', controller: 'ViewStudentsController' }). otherwise({ redirectTo: '/addStudent' }); }]);
The following are important issues to consider in the above example
Example
The following example will demonstrate all the above commands.
testAngularJS.html
<html> <head> <title>Angular JS Views</title> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/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.html"> <h2> Add Student </h2> {{message}} </script> <script type="text/ng-template" id="viewStudents.html"> <h2> View Students </h2> {{message}} </script> </div> <script> var mainApp = angular.module("mainApp", ['ngRoute']); mainApp.config(['$routeProvider', function($routeProvider) { $routeProvider. when('/addStudent', { templateUrl: 'addStudent.html', controller: 'AddStudentController' }). when('/viewStudents', { templateUrl: 'viewStudents.html', 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>
Results
Open textAngularJS.html in your web browser. See the results as follows: