CodeSolution

Please wait...

AngularJs nested routing using ui-routing : 10 minutes

AngularJs nested routing  using ui-routing : 10 minutes

@AngularJs  @ nested  @ ui-routing  @ minutes  

AngularJs

AngularJS is a very powerful JavaScript Framework. It is used in Single Page Application (SPA) projects. It extends HTML DOM with additional attributes and makes it more responsive to user actions.?

 

AngularJS is a structural framework for dynamic web apps. It lets you use HTML as your template language and lets you extend HTML's syntax to express your application's components clearly and succinctly. Angular's data binding and dependency injection eliminate much of the code you currently have to write. 

 

Ui-Routing

AngularJS provides to create a single page applications. When creating single page applications, routing will be very important. Ui-routing provide nested view routing .UI-Router is the defacto standard for routing in AngularJS. UI-Router has become the standard choice for routing non-trivial apps in AngularJS (1.x).The UI-Router is a routing framework for AngularJS.

 

 

Setup

Project Structure 

ui-routing                        // Project directory
    - index.html                  // will hold the main template for our app
    - app.js                      // our angular code
    - angular-ui-router.min.js    // angular-ui-router.min JS
    - angular.min.js              // angularjs File
    - pages                       // view directory
        - header.html             // about page code
        - home.html               // home page code
        - about.html              // about page code
        - contact.html            //  about page code

Create Index.html and add code

<!DOCTYPE html><html>
<head>
<title>Ui-routing Examples</title><link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css">
<style type="text/css">
        .container{
            padding: 0px;
            width: 100%;
         }
</style>
<script type="text/javascript" src="angular.min.js"><script>
<script type="text/javascript" src="angular-ui-router.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-app="UiRouting"><div ui-view></div>
;</body>
</html>

 

Create app.js and add code

var app = angular.module('UiRouting', ['ui.router']);
app.config(function($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('/header/home');
    $stateProvider
     .state('header', {
        url: '/header',
        abstract: true,
        templateUrl: 'pages/header.html'
    })
    .state('header.home', {
        url: '/home',
        views: {
          'container': {
            templateUrl: 'pages/home.html'
          }
        }
    })
    .state('header.about', {
        url: '/about',
        views: {
          'container': {
            templateUrl: 'pages/about.html'
          }
        }
    })
    .state('header.contact', {
        url: '/contact',
        views: {
          'container': {
            templateUrl: 'pages/contact.html'
          }
        }
    });
});

Create header.html and add code

<div><nav class="navbar navbar-inverse" role="navigation">
<div class="navbar-header">
<a class="navbar-brand" ui-sref="header.home">Ui-routing Examples</a>
</div><ul class="nav navbar-nav"><li>
<a ui-sref="header.home">Home></a></li>
<li><a ui-sref="header.about">About></a></li>
<li><a ui-sref="header.contact">Contact></a></li>
</ul>
</nav>
<div ui-view="container"></div>
</div>

Create Other HTML pages like home about contact etc..

Result : 

 

Github Link: https://github.com/Sudarshan101/ui-routing

 

Thank you :)

Comments 0

Reply

>