In this sample we will be just creating a sample structure with very simple features. You have gone through multiple samples, but trying to put things together for creating at least a small structure of multiple components. Let’s consider a case that one will be just having some static pages and header and footer for application.
So we can say that we will be having multiple tabs in application and will also making a tab, which will having drop-down menus. In this sample we will be using controllers just to show how those are written. Will not creating any modules here.Things will be help full for later stages with this sample. Also this sample will be more helpful for beginners and designers mainly. We are using bootstrap css for the responsive design.
Let’s first create index.html of application as below:-
<!DOCTYPE html>
<html>
<head>
<title>SPA with Angular</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<!-- Add jQuery and Bootstrap. -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<!-- Add angularjs -->
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-route.min.js"></script>
<!-- Add application's JS File -->
<script src="js/app.js"></script>
<body ng-app="spaWebApp">
<div ng-include='"header.html"'></div>
<div ng-view=""></div>
<div ng-include='"footer.html"'></div>
</body>
</html>
As you can see that page have below main parts:-
1) Adding jQuery,Bootstrap and angular references.
2) Adding ‘app.js’
3) Added three divs for header,footer and content of application.
Now below is code of app.js:-
var app = angular.module('spaWebApp', [
'ngRoute'
]);
/**
* Configure the Routes.
*/
app.config(['$routeProvider', function ($routeProvider) {
$routeProvider
// Home.
.when("/", {templateUrl: "pages/home.html", controller: "HomeController"})
// Tabs.
.when("/tab1", {templateUrl: "pages/tab1Ui.html", controller: "TabController"})
.when("/tab2", {templateUrl: "pages/tab2Ui.html", controller: "TabController"})
.when("/tab3", {templateUrl: "pages/tab3Ui.html", controller: "TabController"})
// Menu Items.
.when("/tab4/menu1", {templateUrl: "pages/tab4menu1Ui.html", controller: "MenuController"})
.when("/tab4/menu2", {templateUrl: "pages/tab4menu2Ui.html", controller: "MenuController"})
//page not Found.
.otherwise("/404", {templateUrl: "pages/errorpage.html", controller: "PNFController"});
}]);
app.controller('HomeController', function (/* $scope, $location, $http */) {
document.getElementById("msglbl").innerHTML = "HomeController Working Fine.";
});
app.controller('TabController', function (/* $scope, $location, $http */) {
document.getElementById("msglbl").innerHTML = "TabController Working Fine.";
});
app.controller('MenuController', function (/* $scope, $location, $http */) {
document.getElementById("msglbl").innerHTML = "MenuController Working Fine.";
});
app.controller('PNFController', function (/* $scope, $location, $http */) {
document.getElementById("msglbl").innerHTML = "PNFController Working Fine.";
});
For this sample you don’t need to focus a lot on multiple controllers.But you can see how controllers can be associated with multiple parts of application.In this sample we are just changing the text of footer as via multiple controllers.
As per above code you will need to create header and footer templates and multiple pages inside ‘pages’ folder.Instead of providing code of multiple pages just adding sample html for ‘home.html’.You can create all pages as per your requirement.Below is structure for ‘home.html’:-
<div class="container" >
<div class="jumbotron" style="width: 100%;height: 100%" align="center">
This is Home page.
</div>
</div>
Here is ‘header.html’:-
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav navbar-right">
<li><a href="#/">Home</a></li>
<li><a href="#/tab1">Tab1</a></li>
<li><a href="#/tab2">Tab2</a></li>
<li><a href="#/tab3">Tab3</a></li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown">Tab4(with Menus) <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="#/tab4/menu1">Menu1</a></li>
<li><a href="#/tab4/menu2">Menu2</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
and the last one is ‘footer.html’:-
<footer>
<div align="center">
<p>This is your footter.</p>
<p id="msglbl" style="color: #0000ff"></p>
</div>
</footer>
Except these files you will see some pages referenced in app.js. Those are simple pages lot like similar to ‘home.html’.
You can design them later according to your needs later on.
Hope this will be useful for someone.