AngularJS How to Create Routes
Submitted by nurhodelta_17 on Thursday, January 4, 2018 - 19:34.
Getting Started
I've used CDN for Bootstrap, Angular Js, and Angular Route JS so you need internet connection for them to work.Creating our Main Page
First this is our main page named index.html. In here, we're gonna create a simple bootstrap navbar for navigation between routes.- <!DOCTYPE html>
- <html ng-app="app">
- <head>
- <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- <!-- Brand and toggle get grouped for better mobile display -->
- <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>
- <!-- Collect the nav links, forms, and other content for toggling -->
- <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
- <ul class="nav navbar-nav">
- </ul>
- </div><!-- /.navbar-collapse -->
- </div><!-- /.container-fluid -->
- </nav>
- </body>
- </html>
Creating our Templates
This are the pages that we are including in our routes. home.html- This is the Homepage
- This the About Page
- This the Blog Page
angular.js
Lastly, this contains our Angular.js scripts and our router. Take note that in routing, we need to add the dependency ngRoute to our app for it to work.- var app = angular.module('app', ['ngRoute']);
- app.config(function($routeProvider){
- $routeProvider
- .when('/', {
- templateUrl: 'home.html'
- })
- .when('/about', {
- templateUrl: 'about.html'
- })
- .when('/blog', {
- templateUrl: 'blog.html'
- })
- .otherwise({
- redirectTo: '/'
- });
- });
Comments
Add new comment
- Add new comment
- 27 views