Using ng-view and ng-template directives and $routeProvider services you can provide single page application via multiple views on a single page.
ng-view: A place holder where a corresponding view can be placed.
ng-template: Used to create an html view using script tag with the help of “id” attribute.
$routeProvider: Set the configuration of urls and map them with the corresponding html page via attaching controller.
If htm page is not defined then ng-template will be used with id=”addStudent.htm”.
Following example will show the above criteria:
Save the file as angularview.html and run into browser.
<html>
<head>
<title>Angular JS Views</title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-route.min.js"></script>
</head>
<body>
<h2>Angular JS Views</h2>
<div ng-app="angApp">
<p><a href="#addProduct">Add Product</a></p>
<p><a href="#viewProducts">View Products</a></p>
<div ng-view></div>
<script type="text/ng-template" id="addProduct.htm">
<h2> Add Product </h2>
{{message}}
</script>
<script type="text/ng-template" id="viewProducts.htm">
<h2> View Products </h2>
{{message}}
</script>
</div>
<script>
var angApp = angular.module("angApp", ['ngRoute']);
angApp.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/addProduct', {
templateUrl: 'addProduct.htm',
controller: 'addProductController'
}).
when('/viewProducts', {
templateUrl: 'viewProducts.htm',
controller: 'viewProductsController'
}).
otherwise({
redirectTo: '/addProduct'
});
}]);
angApp.controller('addProductController', function($scope) {
$scope.message = "Page template for add product form";
});
angApp.controller('viewProductsController', function($scope) {
$scope.message = "Page template for display all products";
});
</script>
</body>
</html> <div>External add product page</div>
<div>External view product page</div>
Motorcycle accidents often lead to severe injuries because riders have minimal protection compared to drivers…
No business owner starts his or her business with the hope of making losses. Everyone…
Car accidents can be traumatic, and the physical and psychological aftermath of a car accident…
Worker's compensation insurance deals with all those employees that get work-related injury or illness. When…
Mobile commerce is no longer a trend. It is the main stage where buyers make…
The field of education welcomes and recognizes AI's potential to provide a more immersive learning…