Home Blog Work

Navigation between pages in NativeScript using Angular 2 Routers

Published on July 18, 2016

In the previous post, we have seen how to build Side Drawer for a NativeScript application.

In that post, we are displaying the page names on SideDrawer menu but when you click on them it does nothing. We have to implement a navigation mechanism for the app.

To view the source code go here. To download the apk click here. or

If your were developing the app without Angular 2 you have to use ui/frame module.


import frameModule = require("ui/frame");
var topmost = frameModule.topmost();

Since we are developing the application using Angular 2 let’s take the advantage of it. We can use Angular 2 router concept to implement page navigation for NativeScript.

We will implement this on top of the previous code. To know how to get the code please go here and read Get GitUsers App section But you need to checkout v1.1 tag insted v1.0.

Angular Router for Component

In Angular 2 everything is a component. So when we want to go from one page to another page means that we are loading one component based the path match.

Note: We are using @angular/router v3. in this post. For the deprecated beta router please import nativescript-angular/router-deprecated

Adding About Component

In our previous app we have only user list component. Let’s create another component so that we can navigate between those two component.


import { Component, OnInit } from "@angular/core";

  selector: "about",
  templateUrl: "about.component.html"
export class AboutComponent implements OnInit {
  constructor() {}
  ngOnInit() {}


<label text="About Page Text goes here............." textWrap="true"></label>

Router Config

We have to tell angular which component should load into the view at what path or pattern.

To do so we will implement a RouterConfig.

import { RouterConfig } from "@angular/router";
import { nsProvideRouter } from "nativescript-angular/router";
import { userListComponent } from "../components/list/list.component";
import { AboutComponent } from "../components/about/about.component";

const routes: RouterConfig = [
  { path: "", redirectTo: "/list", terminal: true },
  { path: "list", component: userListComponent },
  { path: "about", component: AboutComponent }

export const APP_ROUTER_PROVIDERS = [
  nsProvideRouter(routes, { enableTracing: false })

Here we created a routers constant of Type RouterConfig. In this, we will set the path pattern to component name.

All component have been imported above.

We are exporting APP_ROUTER_PROVIDERS wich is an array of nsProvideRouters.


Now, we need to tell angular to how to use these route config inside the application in orders to do so we have to pass the APP_ROUTER_PROVIDERS to our nativeScriptBootstrap method.


import { nativeScriptBootstrap } from "nativescript-angular/application";
import { AppComponent } from "./app.component";
import { HTTP_PROVIDERS } from "@angular/http";
import { SIDEDRAWER_PROVIDERS } from "nativescript-telerik-ui/sidedrawer/angular";
import { APP_ROUTER_PROVIDERS } from "./router/router";

nativeScriptBootstrap(AppComponent, [

When we go to a specific path we want to load a view in a specific place. Where?

In order to tell angular where to load the view, we will use <router-outlet></router-outlet> directive.


<ActionBar title="GitUsers">
    text="Go Back"
<RadSideDrawer #drawer>
  <template drawerSide>
    <StackLayout class="p bgc-white">
      <ListView [items]="pages" row="1">
        <template let-item="item" let-i="index">
  <template drawerMain>
    <StackLayout class="m">

In above code on which menu item click which Component view to load can be done by using nsRouterLink directive


In app.component.html we are using <ListView> with passing pages as items. We have to update our pages array as

this.pages = [
  { name: "Home", path: "/list" },
  { name: "About", path: "/about" }

Also when you click menu item at the side draw we would want to close slide draw and show only the content. But by defult it won’t happen. We have to look for the route change event and close the the Side Draw when ever the route is changed.

// on every route change close the sideDrawer => {

The final app.component.ts will be.

import { Component, ViewChild, ChangeDetectorRef, OnInit } from "@angular/core";
import {
} from "nativescript-telerik-ui/sidedrawer/angular";
import { userListComponent } from "./components/list/list.component";
import { ROUTER_DIRECTIVES, Router } from "@angular/router";
import { NS_ROUTER_DIRECTIVES } from "nativescript-angular/router";

  selector: "my-app",
  directives: [userListComponent, ROUTER_DIRECTIVES, NS_ROUTER_DIRECTIVES],
  templateUrl: "app.component.html"
export class AppComponent implements OnInit {
  public drawerComponent: RadSideDrawerComponent;
  private drawer: SideDrawerType;
  public pages: Array<Object>;

    private router: Router,
    private _changeDetectionRef: ChangeDetectorRef
  ) {
    this.router = router;

  // on component init
  ngOnInit() {
    // List all pages and there router path here
    this.pages = [
      { name: "Home", path: "/list" },
      { name: "About", path: "/about" }

    // on every route change close the sideDrawer => {

  // After veiw init
  ngAfterViewInit() {
    this.drawer = this.drawerComponent.sideDrawer;

  // on menu icon click
  public openDrawer() {

That’s it we are done. Now you have application with side drawer menu navigation.

Well done!!!