Being that Ionic Framework relies heavily on the UI-Router for navigation, I thought I would do a write-up on how to properly use it.
We use the UI-Router to navigate between view states in our application. By view states, I mean screens composed as template files. If you’re looking for information on navigating in Ionic 2, visit my other post on the subject as it doesn’t use the UI-Router like Ionic Framework 1 does.
Let’s start by creating a new Android and iOS Ionic project:
ionic start IonicProject blank
cd IonicProject
ionic platform add android
ionic platform add ios
Note that if you’re not using a Mac, you cannot add or build for iOS.
The UI-Router operates on application states, where each state might represent a new screen in your application.
Take the following code:
var example = angular.module('starter', ['ionic'])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
// Stuff in here
});
})
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('login', {
url: '/login',
templateUrl: 'templates/login.html',
controller: 'LoginController'
})
.state('users', {
url: '/users',
templateUrl: 'templates/users.html',
controller: 'UserController'
})
.state('user', {
url: "/users/:userId",
templateUrl: "templates/user.html",
controller: "UserController"
});
$urlRouterProvider.otherwise('/users');
});
In the above code we are going to pay attention to the .config()
method. There are three states which represent three screens in our app. We have the following screens:
Now lets break down what each part of the state represents.
Object Key | Description |
---|---|
url | The URL route that can be accessed via href properties |
templateUrl | The path to the view template HTML file |
controller | The controller to be used in this view |
Take the following chunk of code:
.state('user', {
url: "/users/:userId",
templateUrl: "templates/user.html",
controller: "UserController"
})
The template we will use is found in www/templates/user.html in our project. Notice that in our URL we have :userId
. This represents a parameter that will be passed into our controller.
example.controller("UserController", function($scope, $stateParams) {
// $stateParams.userId;
});
So how might all this look from a UI perspective? Let’s start with our index.html file.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title></title>
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="js/app.js"></script>
<script src="cordova.js"></script>
</head>
<body ng-app="digitalocean">
<ion-side-menus>
<ion-side-menu-content>
<ion-nav-bar class="bar-positive"></ion-nav-bar>
<ion-nav-view></ion-nav-view>
</ion-side-menu-content>
</ion-side-menus>
</body>
</html>
The <ion-nav-view>
is where the template will be injected via your UI-Router. So it is now time to create our templates:
<ion-view title="Users">
<ion-content>
<div class="list">
<a ng-repeat="user in users" class="item" href="#/users/{{user.id}}">
{{user.name}}
</a>
</div>
</ion-content>
</ion-view>
The above code is suitable for our www/templates/users.html file. You’ll notice clicking on a list item will pass the user id as one of the parameters.
All your other templates will follow a similar design.
If you’d like to read more on the topic of application states and the UI-Router, you can view the official GitHub documentation.