<script type="application/ld+json">
 "@context": "https://schema.org",
 "@type": "FAQPage",
 "mainEntity": [{
   "@type": "Question",
   "name": "What is a UI-ROUTER?",
   "acceptedAnswer": {
     "@type": "Answer",
     "text": "UI Router is a routing framework for AngularJS, which 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."
   "@type": "Question",
   "name": "What are the functionality provided by UI-Router?",
   "acceptedAnswer": {
     "@type": "Answer",
     "text": "1. UI-Router states.
2. UI-Router nested views.
3. UI-Router named views."
   "@type": "Question",
   "name": "What is a component in AngularJs?",
   "acceptedAnswer": {
     "@type": "Answer",
     "text": "The component is a way to create re-usable isolated entities."
   "@type": "Question",
   "name": "When should a component be used?",
   "acceptedAnswer": {
     "@type": "Answer",
     "text": "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 for components you can use directives for other purposes)."
   "@type": "Question",
   "name": "Why a component should be used?",
   "acceptedAnswer": {
     "@type": "Answer",
     "text": "1. Components have a well-defined lifecycle hook.
2. Components only control their own View and Data.
3. simpler configuration than plain directives.
4. optimized for component-based architecture."

Tech Corner

Component Based Architecture In AngularJs Using UI-Router

Thejeshwar Reddy
Feb 8
4-5 mins

Table of contents

Key takeawaysCollaboration platforms are essential to the new way of workingEmployees prefer engati over emailEmployees play a growing part in software purchasing decisionsThe future of work is collaborativeMethodology

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, which 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: contact.id });

Through view

<a ui-sref="contacts.person({ contactId: {{contact.id}} })">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



Thejeshwar Reddy

Andy is the Co-Founder and CIO of SwissCognitive - The Global AI Hub. He’s also the President of the Swiss IT Leadership Forum.

Andy is a digital enterprise leader and is transforming business strategies keeping the best interests of shareholders, customers, and employees in mind.

Follow him for your daily dose of AI news and thoughts on using AI to improve your business.

Catch our interview with Andy on AI in daily life

Continue Reading