Git Product home page Git Product logo

angular-ui-router-readme's Introduction

What is ui-router?

Overview

ngRoute provides us with basic functionality, but the leading router for Angular is uiRouter. This provides much more, such as nested views, multiple views and a few different offerings. Let's take a look at how uiRouter does things instead.

Objectives

  • Describe ui-router and ui-view
  • Write a route using ui-router
  • Integrate a Controller with ui-router
  • Integrate a template with ui-router

uiRouter

uiRouter is quite similar to ngRoute, but each "route" is known as a "state". We define states, not routes.

Each state that we define is quite similar to a route in ngRoute, but we have to give each state a name too. This allows us to have children states inside of a state (we'll go onto nested states/views soon).

This is awesome - you might've noticed how in the last lab we've got three links to #/user/name. Instead, with uiRouter, we can use the state names instead of URLs. This saves us time in the long run - if we have 100 different places in our app that link to the user page, imagine having to update all of them to the URL if it ever changed! However, as we've got state names, we won't have to update any of them.

Let's take a look at how we'd define a route in ngRoute:

angular
	.module('app', ['ngRoute'])
	.config(function ($routeProvider) {
		$routeProvider
			.when('/user', {
				templateUrl: 'views/user.html',
				controller: 'UserController'
			});
	});

And how we'd define a route in uiRouter:

angular
	.module('app', ['ui.router'])
	.config(function ($stateProvider) {
		$stateProvider
			.state('user', {
				url: '/user',
				templateUrl: 'views/user.html',
				controller: 'UserController'
			});
	});

You can see they're very similar. We've moved the URL into the configuration object, and replaced the URL with the state's name. We're also using $stateProvider instead of $routeProvider.

Instead of using ng-view, we use ui-view to place the states in our HTML.

We'll go into more advanced concepts with uiRouter soon - things such as nested views and resolving data.

uiSref

uiRouter also provides a directive named ui-sref (as in, $state.href()). We mentioned earlier about linking to states by name - this is where uiSref comes in. We can use this on our hyperlinks to generate links to our states. We pass in the state name as the value and uiRouter will replace the link with a link to our state.

Say we've got a state named docs, that links to /documentation/v1:

angular
	.module('app', ['ui.router'])
	.config(function ($stateProvider) {
		$stateProvider
			.state('docs', {
				url: '/documentation/v1',
				templateUrl: 'views/user.html',
				controller: 'UserController'
			});
	});

We can then use ui-sref="docs" to link to that state.

<a href="" ui-sref="docs">Go to the docs!</a>

This will then get automatically changed into:

<a href="/documentation/v1">Go to the docs!</a>

This is awesome - we can change the URL on our states without changing the name and all of our links get updated to match the new URL.

uiSrefActive

uiRouter also provides us with one more directive - ui-sref-active. We pass in a class name as the value, and when the given state inside our ui-sref is active, it will apply this class to the link.

For instance, if we have this:

<a href="" ui-sref="docs" ui-sref-active="active">Go to the docs!</a>

When we're actually on the docs page, that link will get the active class applied to it:

<a href="" class="active" ui-sref="docs" ui-sref-active="active">Go to the docs!</a>

Simple!

angular-ui-router-readme's People

Contributors

bhollan avatar ipc103 avatar pletcher avatar toddmotto avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.