Tech Corner

Component Based Architecture In AngularJs Using UI-Router

Thejeshwar Reddy
last edited on
May 2, 2023
4-5 mins

Table of contents

Automate your business at $5/day with Engati

Switch to Engati: Smarter choice for WhatsApp Campaigns 🚀
AngularJs Using UI-Router

There's no doubt in the fact that one who is going to start writing a new app today would not be choosing angularJs framework, however thousands of AngularJs applications do exist as many companies who have started off with it use it till today, safe to say AngularJs isn’t going anywhere.

Most of the application examples I see in AngularJs are mostly in a controller-view structure (using ngRoute), we can achieve better functionality when we couple components and ui-router modules, Developers from later frameworks (Angular, Vue, React) would be familiar with this architecture, It gives powerful functionality compared to ngRoute. Let's get started.

To access the full source code, click here.

What is a UI-ROUTER?

UI Router is a routing framework for AngularJS, whicha allows you to organize the parts, or components of your interface into a state machine. Unlike the ngRoute module, which is organized around URL routes.

Let us take a small example of contact directory app:

  • List of contacts (contactsComponent)
  • Messages for a contact selected (messagesComponent)
  • Detail about a contact selected (detailsComponent)

UI-Router(state) Vs Ng-Route

In state approach, the views are tied to the state, whereas in ng-route, views are tied to the browser url. Here's an example:

  • ngRouteConsider the route transition from /contacts?contactId=1 to /contacts?contactId=2.When the transition happens browser considers this as a complete url change and because the browser considers this as a new url even the contacts.List may be re rendered (and APIs required to load the contacts list) along with the messages and details which is not required (Extra load).
  • UI-RouterNow lets consider the same case with ui-router — component

The componen
The components of AngularJs

In this case, we can selectively load only the messages component and details component when ever the contactId changes, avoiding to re render the contacts component ie you can load only the components tied to that particular state avoiding to load the parent components.

Functionality provided by UI-Router

  • States
  • Nested states, nested views (In ngRoute only one view is present that is why even param change is considered as a route change and whole view is re rendered)
  • Named views
  • Transition hooks (not discussed in this article very useful though)

UI-Router states

  • A state is a particular position in entire navigation system of the application
  • In UI-Router consider the entire application as a tree of states, only one state can be active at a time
  • You can move from any state to any other state in the state tree

UI-Router nested views

A nested view is a child view to a particular state,




Here both info and list are the child views (nested views) of /home, and you can activate any one state and the corresponding view is rendered

UI-Router named views

Lets say you have two components both should be rendered for a particular state then we need to name the views so that we can configure to say which component should be rendered in which view.

Here's an example:

/contacts/{contactId} which is a child state to /contacts

Here we have two components — messagesComponent and detailsComponent which need to be rendered then we name each nested view with a name. The name of a view is given using name attribute, something like this,

<ui-view name=”messageView” ></ui-view>

<ui-view name=”detailsView”></ui-view>

Then, you have to configure to render which component in which view, like this,

<ui-view name=”messageView” ></ui-view>

<ui-view name=”detailsView”></ui-view>

Finally, configure to render which component in which view, like this,

views: {

‘messageView@contacts’: ‘messagesComponent’,

‘detailsView@contacts’: ‘detailsComponent’

Components - What, When, Why, and How

What is a component in AngularJs?

Component is a way to create re-usable isolated entities. It's the same as a directive. The main difference being that it is always isolated scope unlike a directive where u can choose whether it should be isolated or not.

The reason a component is always isolated is because, in a large application when $scope Inheritance or watchers are in play, inevitably there will be problems. Ot will be difficult to know which part of the application has actually updated a particular variable, here components works better(isolated scope).

When should a component be used

A component should be used when you are writing an application in the component architecture(I would say if the application is not a component based one no need of components you can use directives for other purposes).

A component is restricted to 'E', so if u want a directive which needs to be triggered by Attribute or Class use directives instead.

Why a component should be used

  • Components have a well-defined lifecycle hooks
  • Components only control their own View and Data
  • Simpler configuration than plain directives
  • Optimised for component-based architecture

How a component is created

angular.module(‘angularApp’).component(‘messagesComponent’, { templateUrl: ‘/client/components/messages/messages.html’, controller: messagesController,

bindings: {

   contact: ‘<’



Lets see some implementation of contacts directory app (mentioned above),

We have three components (refer the component hierarchy image above)

  • Contacts component
  • Messaging component
  • Three
  • Details component

Adding routes and states

Lets add /contacts route and name the state as contacts


var app = angular.module('angularApp', ['ui.router']);

app.config(function ($stateProvider, $urlRouterProvider) {


 name: 'contacts',

 url: '/contacts',

 component: 'contactsComponent'




// main.html



We have created a <ui-view/> in main.html which is the main view of whole application.

When contacts state is active then the contactsComponent is rendered in the <ui-view/> of main.html

Now lets add a child route to /contacts with named views



name: 'contacts.person',

url: '/{contactId}',

views: {

 'messageView@contacts': 'messagesComponent',

 'detailsView@contacts': 'detailsComponent'


resolve: {

contact: function (contactsFactory, $transition$) {







<ui-view name="messageView"></ui-view>

<ui-view name="detailsView"></ui-view>


We have created contacts.person which is a syntax(dot operator) to append child state to a parent state so now person is a child state to contacts.

We have created two <ui-view/> tags and have given each view a name

Now in views property we have mentioned

‘messageView@contacts’: ‘messagesComponent’,

Which is to say to render messagesComponent in the view with name messageView. So, now when person state is activated then the components are rendered to the respective views mentioned in views property

How to navigate to a particular state

Through controller

$state.go(‘contacts.person’, { contactId: });

Through view

<a ui-sref="contacts.person({ contactId: {{}} })">Contacts</a>


I would suggest to build the application in this model itself, for better performance, fast and clean application.

  • Component support has been added in AngularJs from version 1.5, probably as the new frameworks follow the component architecture by default.
  • UI-Router has been developed by Angular team itself so you can depend on it, not worrying about the quality of the library.

Repo for the complete source code

Check out this 55+ tech podcasts to know more on how digital devices have change our lives.

3x Your Revenue With Chatbot And Live Chat
install for free

Thejeshwar Reddy

Close Icon
Request a Demo!
Get started on Engati with the help of a personalised demo.
This is some text inside of a div block.
This is some text inside of a div block.
This is some text inside of a div block.
This is some text inside of a div block.
*only for sharing demo link on WhatsApp
Thanks for the information.
We will be shortly getting in touch with you.
Oops! something went wrong!
For any query reach out to us on
Close Icon
Congratulations! Your demo is recorded.

Select an option on how Engati can help you.

I am looking for a conversational AI engagement solution for the web and other channels.

I would like for a conversational AI engagement solution for WhatsApp as the primary channel

I am an e-commerce store with Shopify. I am looking for a conversational AI engagement solution for my business

I am looking to partner with Engati to build conversational AI solutions for other businesses

Close Icon
You're a step away from building your Al chatbot

How many customers do you expect to engage in a month?

Less Than 2000


More than 5000

Close Icon
Thanks for the information.

We will be shortly getting in touch with you.

Close Icon

Contact Us

Please fill in your details and we will contact you shortly.

This is some text inside of a div block.
This is some text inside of a div block.
This is some text inside of a div block.
This is some text inside of a div block.
This is some text inside of a div block.
Thanks for the information.
We will be shortly getting in touch with you.
Oops! Looks like there is a problem.
Never mind, drop us a mail at