-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12-Views.html
More file actions
112 lines (89 loc) · 4.16 KB
/
12-Views.html
File metadata and controls
112 lines (89 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>AngularJS - Views</title>
<script src="js/lib/angular.min.js"></script>
<script src="js/lib/angular-route.min.js"></script>
<link href="css/estilo.css" rel="stylesheet">
</head>
<body ng-app="mainApp">
<div>
<h3>AngularJS - Views</h3>
<p>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.</p>
</div>
<div>
<h3>ng-view</h3>
<p>ng-view tag simply creates a place holder where a corresponding view (html or ng-template view) can be placed based on the configuration.</p>
<h4>Usage</h4>
<p>Define a div with ng-view within the main module.</p>
<span ng-include="'subpage/1201-Views-ngview.html'"></span>
</div>
<div>
<h3>ng-template</h3>
<p>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. </p>
<h4>Usage</h4>
<p>Define a script block with type as ng-template within the main module.</p>
<span ng-include="'subpage/1202-Views-ngtemplate.html'"></span>
</div>
<div>
<h3>$routeProvider</h3>
<p>$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.</p>
<h4>Usage</h4>
<p>Define a script block with type as ng-template within the main module.</p>
<span ng-include="'subpage/1203-Views-routeProvider.html'"></span>
<h4>Usage</h4>
<p>Define a script block with main module and set the routing configuration.</p>
<span ng-include="'subpage/1203-Views-routeProvider2.html'"></span>
</div>
<p>Following are the important points to be considered in above example.</p>
<ul>
<li>$routeProvider is defined as a function under config of mainApp module using key as '$routeProvider'.</li>
<li>$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.</li>
<li>"otherwise" is used to set the default view.</li>
<li>"controller" is used to set the corresponding controller for the view.</li>
</ul>
<h3>Example</h3>
<h2>AngularJS Sample Application</h2>
<div>
<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>
<p>{{message}}</p>
</script>-->
<script type="text/ng-template" id="viewStudents.htm">
<h2> View Students </h2>
<p>{{message}}</p>
</script>
</div>
<script>
var mainApp = angular.module("mainApp", ['ngRoute']);
mainApp.config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/addStudent', {
templateUrl: 'views/1201-Views-addStudent.html',
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>