ui-router
The de-facto solution to flexible routing with nested views in AngularJS
Top Related Projects
Routing and navigation for your React Native apps
Declarative routing for React
🚦 The official router for Vue 2
Quick Overview
UI-Router is a routing framework for AngularJS and Angular applications. It provides a state machine to manage the application's user interface, allowing for nested views and more powerful routing capabilities than Angular's built-in router.
Pros
- Supports nested views and states, enabling complex UI structures
- Provides a flexible and powerful state machine for managing application state
- Allows for URL routing as well as abstract states without URLs
- Supports lazy loading of modules and components
Cons
- Steeper learning curve compared to Angular's built-in router
- Can be overkill for simple applications with basic routing needs
- Requires additional configuration and setup compared to the default router
- Some developers find the concept of states and nested views confusing at first
Code Examples
- Defining a simple state:
$stateProvider.state('home', {
url: '/home',
templateUrl: 'home.html',
controller: 'HomeController'
});
- Nested states:
$stateProvider
.state('parent', {
url: '/parent',
templateUrl: 'parent.html'
})
.state('parent.child', {
url: '/child',
templateUrl: 'child.html'
});
- Using ui-sref for navigation:
<a ui-sref="home">Home</a>
<a ui-sref="parent.child">Child State</a>
- Resolving data before entering a state:
$stateProvider.state('users', {
url: '/users',
templateUrl: 'users.html',
controller: 'UsersController',
resolve: {
users: function(UserService) {
return UserService.getAllUsers();
}
}
});
Getting Started
-
Install UI-Router:
npm install @uirouter/angularjs
-
Include UI-Router in your Angular module:
angular.module('myApp', ['ui.router']);
-
Configure your states:
angular.module('myApp').config(function($stateProvider) { $stateProvider .state('home', { url: '/home', templateUrl: 'home.html' }) .state('about', { url: '/about', templateUrl: 'about.html' }); });
-
Use ui-view directive in your HTML:
<div ui-view></div>
Competitor Comparisons
Pros of reach/router
- Simpler API and more intuitive routing configuration
- Built-in accessibility features for screen readers
- Smaller bundle size, optimized for React applications
Cons of reach/router
- Limited to React applications, while ui-router supports multiple frameworks
- Less extensive documentation and community support
- Fewer advanced features for complex routing scenarios
Code Comparison
reach/router:
import { Router, Link } from "@reach/router"
const App = () => (
<Router>
<Home path="/" />
<Dashboard path="dashboard" />
</Router>
)
ui-router:
$stateProvider
.state('home', {
url: '/',
templateUrl: 'home.html'
})
.state('dashboard', {
url: '/dashboard',
templateUrl: 'dashboard.html'
});
Key Differences
- reach/router uses a declarative approach with React components
- ui-router uses a configuration object to define routes
- reach/router automatically handles focus management for accessibility
- ui-router offers more flexibility for complex state management
Use Cases
- Choose reach/router for React-based projects prioritizing simplicity and accessibility
- Opt for ui-router in Angular applications or when requiring advanced routing features
Routing and navigation for your React Native apps
Pros of react-navigation
- Designed specifically for React Native, offering seamless integration with mobile app development
- Provides a more declarative API, making it easier to define and manage navigation structures
- Offers built-in support for deep linking and web browser-like navigation
Cons of react-navigation
- Limited to React Native applications, whereas ui-router can be used in various Angular-based projects
- Steeper learning curve for developers not familiar with React Native ecosystem
- May require additional configuration for complex navigation scenarios
Code Comparison
ui-router (Angular):
$stateProvider
.state('home', {
url: '/home',
templateUrl: 'home.html',
controller: 'HomeController'
})
react-navigation (React Native):
const Stack = createStackNavigator();
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
</Stack.Navigator>
Both libraries provide ways to define routes and navigation structures, but react-navigation's approach is more aligned with React's component-based architecture. ui-router uses a more configuration-based approach typical of Angular applications. While ui-router is versatile across different Angular projects, react-navigation is tailored specifically for React Native mobile app development, offering native-like navigation experiences out of the box.
Declarative routing for React
Pros of React Router
- Simpler API and more intuitive routing concepts
- Better integration with React's component-based architecture
- More active development and community support
Cons of React Router
- Less powerful state management capabilities
- Limited support for nested views and complex layouts
- Steeper learning curve for developers new to React ecosystem
Code Comparison
UI-Router (AngularJS):
$stateProvider
.state('home', {
url: '/home',
templateUrl: 'home.html',
controller: 'HomeController'
})
React Router:
<Route path="/home" element={<Home />} />
Key Differences
- UI-Router is designed for AngularJS, while React Router is tailored for React applications
- UI-Router uses a state-based approach, whereas React Router focuses on component-based routing
- React Router's syntax is more declarative and aligns closely with React's JSX
Use Cases
- UI-Router: Complex enterprise applications with nested views and state management requirements
- React Router: Modern React applications prioritizing simplicity and component-based architecture
Community and Ecosystem
- UI-Router: Mature but less active community, primarily for AngularJS projects
- React Router: Larger, more active community with frequent updates and extensive third-party integrations
🚦 The official router for Vue 2
Pros of vue-router
- Simpler and more intuitive API, making it easier to learn and use
- Better integration with Vue.js ecosystem and tooling
- More flexible nested routing capabilities
Cons of vue-router
- Less mature and battle-tested compared to ui-router
- Fewer advanced features for complex routing scenarios
- Smaller community and ecosystem
Code Comparison
vue-router:
const router = new VueRouter({
routes: [
{ path: '/user/:id', component: User }
]
})
ui-router:
$stateProvider.state('user', {
url: '/user/:id',
template: '<user></user>',
controller: 'UserController'
});
Both vue-router and ui-router are popular routing solutions for their respective frameworks, Vue.js and AngularJS. vue-router offers a more straightforward approach to routing, with a focus on simplicity and integration with Vue.js. It provides an easy-to-use API and works seamlessly with Vue's component-based architecture.
ui-router, on the other hand, has been around longer and offers more advanced features for complex routing scenarios. It has a larger community and ecosystem, which can be beneficial for finding solutions to specific problems.
The code comparison shows the difference in syntax and configuration between the two routers. vue-router uses a more declarative approach with a single configuration object, while ui-router relies on a more imperative style with separate method calls for defining states.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
AngularUI Router
Note: this is the Angular 1.x source for UI-Router version 1.x. If you are looking for the source for UI-Router version 0.x, it can be found here
The de-facto solution to flexible routing in angular
Tutorials | API Docs | Download stable (or Minified) | Guide | Sample App | FAQ | Report an Issue | Contribute | Help! |
Angular UI-Router is a client-side Single Page Application routing framework for AngularJS.
Routing frameworks for SPAs update the browser's URL as the user navigates through the app. Conversely, this allows changes to the browser's URL to drive navigation through the app, thus allowing the user to create a bookmark to a location deep within the SPA.
UI-Router applications are modeled as a hierarchical tree of states. UI-Router provides a state machine to manage the transitions between those application states in a transaction-like manner.
Get Started
Resources
- In-Depth Guide
- Slides comparing ngRoute to ui-router
- UI-Router Extras / Addons for legacy (0.x) (@christopherthielen)
Videos
- Introduction Video (egghead.io)
- Tim Kindberg on Angular UI-Router
- Activating States (egghead.io)
- Learn Angular.js using UI-Router (LearnCode.academy)
Reporting issues and Contributing
Please read our Contributor guidelines before reporting an issue or creating a pull request.
Top Related Projects
Routing and navigation for your React Native apps
Declarative routing for React
🚦 The official router for Vue 2
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot