AngularUI Router Deployment and Usage Guide
Prerequisites
- Node.js (version 10 or higher) and npm
- AngularJS (version 1.x) - UI-Router is designed for AngularJS applications
- Browser with ES5 support (for production deployments)
Installation
Installing UI-Router in your AngularJS project
-
Via npm (recommended):
npm install @uirouter/angularjs -
Via CDN (for quick prototyping):
<script src="https://unpkg.com/@uirouter/angularjs@latest/release/angular-ui-router.js"></script> <!-- or minified version --> <script src="https://unpkg.com/@uirouter/angularjs@latest/release/angular-ui-router.min.js"></script> -
Add as a dependency to your AngularJS module:
angular.module('myApp', ['ui.router']);
Configuration
Basic State Configuration
Configure states in your AngularJS application using the $stateProvider:
angular.module('myApp', ['ui.router'])
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('home', {
url: '/',
template: '<h1>Home</h1>'
})
.state('about', {
url: '/about',
template: '<h1>About</h1>'
});
$urlRouterProvider.otherwise('/');
});
HTML Configuration
Use the ui-view directive to define viewports:
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.0/angular.min.js"></script>
<script src="https://unpkg.com/@uirouter/angularjs@latest/release/angular-ui-router.js"></script>
<script src="app.js"></script>
</head>
<body>
<a ui-sref="home">Home</a>
<a ui-sref="about">About</a>
<div ui-view></div>
</body>
</html>
Build & Run
Development
-
Set up a basic AngularJS project structure:
my-app/ ├── index.html ├── app.js └── package.json -
Run a local development server:
# Using http-server (install globally if needed) npm install -g http-server http-server -o -
Or use a build tool like webpack or gulp:
# With webpack-dev-server npm install --save-dev webpack webpack-dev-server webpack serve --mode development
Production
-
Build your application:
# With webpack webpack --mode production -
Serve the built files:
# Using http-server http-server dist -p 8080
Deployment
Static Hosting (Recommended for SPA)
UI-Router applications are Single Page Applications (SPAs) that work well with static hosting:
-
Vercel (formerly Zeit Now):
npm install -g vercel cd dist vercel -
Netlify:
npm install -g netlify-cli cd dist netlify deploy --prod -
GitHub Pages:
# Build and deploy to gh-pages branch npm run build git subtree push --prefix dist origin gh-pages
Server-Side Rendering (Advanced)
For server-side rendering, consider using AngularJS with a backend framework:
- Express.js with AngularJS:
const express = require('express'); const app = express(); const path = require('path'); app.use(express.static('dist')); app.get('*', (req, res) => { res.sendFile(path.join(__dirname, 'dist', 'index.html')); }); app.listen(3000, () => console.log('Server running on port 3000'));
Troubleshooting
Common Issues and Solutions
-
State not loading:
- Ensure the state is properly registered in the config block
- Check that the
ui-viewdirective is present in your template - Verify the state name in
ui-srefmatches the registered state
-
404 errors on page refresh:
- Configure your server to always serve
index.htmlfor unknown routes - For static hosting, use
_redirectsfile (Netlify) orvercel.json(Vercel):{ "rewrites": [{ "source": "/(.*)", "destination": "/index.html" }] }
- Configure your server to always serve
-
Dependency injection issues:
- Use array notation for minification-safe dependency injection:
$stateProvider.state('home', { controller: ['$scope', function($scope) { // controller code }] });
- Use array notation for minification-safe dependency injection:
-
UI-Router not loading:
- Check that the script tag is included before your app.js
- Verify the AngularJS module dependency is correctly specified
- Check browser console for JavaScript errors
-
Nested views not working:
- Ensure parent states have
ui-viewdirectives - Check that nested states are properly configured with parent references
- Verify the state hierarchy in your configuration
- Ensure parent states have
Additional Resources
- API Documentation: https://ui-router.github.io/docs/latest/
- Tutorials: https://ui-router.github.io/tutorials/
- Sample App: http://ui-router.github.io/resources/sampleapp/
- FAQ: https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions