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">
ng-template... <div ng-view></div> </div> 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. Define a script block with type as ng-template within the main module.
<div ng-app = "mainApp">
$routeProvider...... <script type = "text/ng-template" id = "addEmp.htm"> <h2> Add An Employee </h2> {{message}} </script> </div> $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. Define a script block with type as ng-template within the main module.
<div ng-app = "mainApp">
Define a script block with type as ng-template within the main module.
... <script type = "text/ng-template" id = "addEmp.htm"> <h2> Add An Employee </h2> {{message}} </script> </div>
<div ng-app = "mainApp">
Define a script block with main module and set the routing configuration.<script type = "text/ng-template" id = "addEmp.htm"> <h2> Add An Employee</h2> {{message}} </script></div> var mainApp = angular.module("mainApp", ['ngRoute']);
mainApp.config(['$routeProvider', function($routeProvider) {
$routeProvider. when('/addEmp', { templateUrl: 'addEmp.htm', controller: 'AddEmpController' }). when('/viewEmp', { templateUrl: 'viewEmp.htm', controller: 'ViewEmpController' }). otherwise({ redirectTo: '/addEmp' }); }]); |