Angular JS Routing using UI-Router
Submitted by nurhodelta_17 on Sunday, January 14, 2018 - 21:08.
Getting Started
I've used CDN for Bootstrap, Angular JS and UI-Router so you need internet connection for them to work.Why choose ui-router rather than angular-route?
You can visit this page if you want a more detailed instruction on the difference of the two but to sum up, ui-router can provide nested views and multiple views.index.html
This is the main page of our app.- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
- </head>
- <body ng-app="app">
- <nav class="navbar navbar-default">
- <div class="container">
- <div class="navbar-header">
- <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
- </button>
- </div>
- <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
- <ul class="nav navbar-nav">
- </ul>
- </div>
- </div>
- </nav>
- <div class="container">
- <div class="row">
- <div class="text-center">
- </div>
- </div>
- </div>
- </body>
- </html>
app.js
This is the main angular js script of our app.- var app = angular.module('app', ['ui.router']);
- app.config(function($stateProvider, $urlRouterProvider) {
- $urlRouterProvider.otherwise('/home');
- $stateProvider
- // HOME STATES AND NESTED VIEWS
- .state('home', {
- url: '/home',
- templateUrl: 'partials/home.html'
- })
- // nested with custom controller
- .state('home.anime', {
- url: '/anime',
- templateUrl: 'partials/anime.html',
- controller: function($scope) {
- $scope.animes = ['Shokugeki no Soma', 'Haikyuu', 'Nanatsu no Tazai', 'Asterisk'];
- }
- })
- // nested with external controller
- .state('home.movie', {
- url: '/movie',
- templateUrl: 'partials/movie.html',
- controller: 'movieCtrl'
- })
- // nested with string data
- .state('home.quote', {
- url: '/quote',
- template: 'Chance favors the prepared mind',
- })
- // ABOUT PAGE AND MULTIPLE NAMED VIEWS
- .state('about', {
- url: '/about',
- views: {
- '': { templateUrl: 'partials/about.html' },
- 'firstColumn@about': { template: 'This is the first column!' },
- 'secondColumn@about': {
- templateUrl: 'partials/second_table.html',
- controller: 'dataCtrl'
- }
- }
- })
- // SIMPLE PAGE
- .state('blog', {
- url: '/blog',
- templateUrl: 'partials/blog.html',
- controller: 'blogCtrl'
- });
- });
movieController.js
This is the controller for our movie.html- 'use strict';
- app.controller('movieCtrl', function($scope) {
- $scope.movies = ['Marvels Movies', 'DC Movies', 'Science Fiction', 'Detective Movies'];
- });
dataController.js
This is the controller for our second_table.html- 'use strict';
- app.controller('dataCtrl', function($scope) {
- $scope.friends = [
- {
- firstname: 'Gemalyn',
- lastname: 'Cepe'
- },
- {
- firstname: 'Julyn',
- lastname: 'Divinagracia'
- },
- {
- firstname: 'Rupert',
- lastname: 'Dikoalam'
- }
- ];
- });
blogController.js
This is the controller for our blog.html- 'use strict';
- app.controller('blogCtrl', function($scope) {
- $scope.blogs = [
- {
- title: 'Angular JS Crud Operation',
- link: 'https://www.sourcecodester.com/php/11909/angularjs-crud-search-sort-and-pagination-phpmysqli.html'
- },
- {
- title: 'Angular JS Progress Bar',
- link: 'https://www.sourcecodester.com/tutorials/javascript/11928/angularjs-progress-bar-phpmysqli.html'
- },
- {
- title: 'Angualr JS Deleting Multiple Rows',
- link: 'https://www.sourcecodester.com/tutorials/javascript/11929/angularjs-delete-multiple-rows-using-phpmysqli.html'
- }
- ];
- });
home.html
As per app.js script code, this is treated as our index page.anime.html
This is one of the nested views.movie.html
This is the other nested view.about.html
This is our about page that contains multiple views.second_table.html
This is one of the multiple views.blog.html
Lastly, this is our blog page that contains a simple view.Add new comment
- 78 views