SlideShare a Scribd company logo
1 of 38
Download to read offline
ui-router and $state
gabe scholz
👏👏👏😁
who has heard of ui-router?
📈
what is the role of a controller?
🎉🎈🎊
what is the role of a model?
ui-router > ngRoute
!
using routing to control state
agenda 😍
If you’re using ngRoute
you might be hacking state
into your application.
👎
routes
views
dependencies
state
bonus features
routes
views
dependencies
state
bonus features
routes
views
dependencies
state
bonus features
routes
views
dependencies
state
bonus features
routes
views
dependencies
state
bonus features
angular.module(‘app’, [‘ngRoute’, ‘ui.router’])
!
.config(function ($routeProvider, $stateProvider)
{
// DO SOME STUFF!👌
});
housekeeping 🏡
housekeeping 🏡
Presentation
id: 1
Slide
id: 6
presentation_id: 1
Slide
id: 6
presentation_id: 1
Slide
id: 6
presentation_id: 1
Slide
id: 1,2,3,4
presentation_id: 1
/presentation/:presentation_id/show
!
/presentation/:presentation_id/slide/:slide_id
$routeProvider
.when(‘/presentation/:id/show’, {
templateUrl: ‘presentation.tpl.html',
controller: ‘PresentationCtrl’,
…
});
!
$stateProvider
.state(‘presentation’, {
url: ’/presentation/:id/show’,
templateUrl: ‘presentation.tpl.html',
controller: ‘PresentationCtrl’,
…
});
routes 🚗
routes views dependencies state bonus
$routeProvider
.when(‘/presentation/:id/show’, {
templateUrl: ‘presentation.tpl.html',
controller: ‘PresentationCtrl’,
…
})
.when(‘/presentation/:id/slides/:slide_id’, {
templateUrl: ‘presentationSlide.tpl.html’,
controller: ‘PresentationSlideCtrl’,
…
});
routes 🚗
routes views dependencies state bonus
$stateProvider
.state(‘presentation’, {
url: ’/presentation/:id’,
templateUrl: ‘presentation.tpl.html',
…
})
.state(‘presentation.show’, {
url: ’/show’,
templateUrl: ‘presentationShow.tpl.html',
controller: ‘PresentationCtrl’,
…
})
.state(‘presentation.slide’, {
url: ‘/slide/:slide_id’,
templateUrl: ‘presentationSlide.tpl.html’,
controller: ‘PresentationSlideCtrl’,
…
});
routes 🚗
routes views dependencies state bonus
views 📺
presentation.tpl.html presentationSlide.tpl.html
<div ng-view></div><div ng-view></div>
routes views dependencies state bonus
views 📺
presentation.tpl.html
!
$stateProvider
.state(‘presentation’, {
url: ’/presentation/:id’,
templateUrl:
‘presentation.tpl.html’,
…
}); <div ui-view></div>
<div ui-view></div>
<div class=‘titleBar’>…</div>
<div ui-view></div>
routes views dependencies state bonus
views 📺
presentation.tpl.html
!
$stateProvider
.state(‘presentation.slide’, {
url: ‘/slide/:slide_id’,
controller: …
templateUrl:
‘presentationSlide.tpl.html’
…
});
<div ui-view></div>
presentationSlide.tpl.html
<div class=‘titleBar’>…</div>
<div ui-view></div>
routes views dependencies state bonus
views 📺
presentation.tpl.html
!
$stateProvider
.state(‘presentation’, {
url: ’/presentation/:id’,
controller: …
templateUrl:
‘presentation.tpl.html’,
…
});
<div ui-view></div>
<div ui-view=‘sideBar’></div>
<div ui-view=‘mainContent’></div>
<div class=‘titleBar’>…</div>
<div ui-view></div>
routes views dependencies state bonus
presentation.tpl.html
!
views 📺
mainContentsideBar
$stateProvider
.state(‘presentation.slide’, {
url: ’/slide/:slide_id’,
views: {
???
},
…
});
<div class=‘titleBar’>…</div>
<div ui-view></div>
routes views dependencies state bonus
views 📺
$stateProvider
.state(‘presentation.slide’, {
url: ’/slide/:slide_id’,
views: {
sideBar: {
controller: …
templateUrl:
‘sideBar.tpl.html’
}
},
…
});
presentation.tpl.html
!
mainContent
sideBar.
tpl.html
<div class=‘titleBar’>…</div>
<div ui-view></div>
routes views dependencies state bonus
views 📺
$stateProvider
.state(‘presentation.slide’, {
url: ’/slide/:slide_id’,
views: {
sideBar: {
controller: …
templateUrl:
‘sideBar.tpl.html’
},
mainContent: {
controller: …
templateUrl:
‘mainContent.tpl.html’
}
},
…
})
presentation.tpl.html
!
mainContent.tpl.html
sideBar.
tpl.html
<div class=‘titleBar’>…</div>
<div ui-view></div>
routes views dependencies state bonus
views 📺
$stateProvider
.state(‘presentation.show’, {
url: ’/show’,
views: {
sideBar: {
template: ‘’
},
mainContent: {
controller: …
templateUrl:
‘showContent.tpl.html’
}
},
…
});
presentation.tpl.html
!
showContent.tpl.html
<div class=‘titleBar’>…</div>
<div ui-view></div>
routes views dependencies state bonus
$routeProvider
.when(‘/presentation/:id/show’, {
…
resolve: {
presentation: function ($routeParams, PresentationApi) {
return PresentationApi.get($routeParams.id);
}
}
});
!
$stateProvider
.state(‘presentation’ {
url: ‘presentation/:id’,
…
resolve: {
presentation: function ($stateParams, PresentationApi) {
return PresentationApi.get($stateParams.id);
}
}
});
dependencies 👶
routes views dependencies state bonus
angular.module(‘app’, [‘ngRoute’])
!
.config(function ($routeProvider) {
$routeProvider
.when(‘/presentation/:id/show’, {
…
resolve: {
presentation: function ($routeParams, PresentationApi) {
return PresentationApi.get($routeParams.id);
}
}
});
})
!
.controller(‘PresentationCtrl’, function ($scope, presentation) {
$scope.presentation = presentation;
});
resolve aside 👯
routes views dependencies state bonus
angular.module(‘app’, [‘ngRoute’])
!
.config(function($routeProvider) {
$routeProvider
.when(‘/presentation/:id/show’, {
…
resolve: {
presentation: function ($routeParams, PresentationApi) {
return PresentationApi.get($routeParams.id);
}
}
})
.when(‘/presentation/:id/slides/:slide_id’, {
…
resolve: {
presentation: function ($routeParams, PresentationApi) {
return PresentationApi.get($routeParams.id);
}
slide: … ???
}
});
});
!
.controller(‘PresentationSlideCtrl’, function ($scope, slide) {
$scope.slide = slide;
});
dependencies 👶
routes views dependencies state bonus
angular.module(‘app’, [‘ngRoute’])
!
.controller(‘PresentationSlideCtrl’,
function ($scope, slide) {
$scope.slide = slide;
});
!
.controller(‘PresentationSlideCtrl’,
function ($scope, $routeParams) {
$scope.slide = SlideApi.get($routeParams.slide_id);
});
!
.controller(‘PresentationSlideCtrl’,
function ($scope, $routeParams, presentation) {
$scope.slide =
presentation.getSlideById($routeParams.slide_id);
});
dependencies 👶
routes views dependencies state bonus
angular.module(‘app’, [‘ngRoute’])
!
.controller(‘PresentationSlideCtrl’,
function ($scope, slide) {
$scope.slide = slide;
});
!
.controller(‘PresentationSlideCtrl’,
function ($scope, $routeParams) {
$scope.slide = SlideApi.get($routeParams.slide_id);
});
!
.controller(‘PresentationSlideCtrl’,
function ($scope, $routeParams, presentation) {
$scope.slide =
presentation.getSlideById($routeParams.slide_id);
});
dependencies 👶
routes views dependencies state bonus
angular.module(‘app’, [‘ngRoute’])
!
.controller(‘PresentationSlideCtrl’,
function ($scope, slide) {
$scope.slide = slide;
});
!
.controller(‘PresentationSlideCtrl’,
function ($scope, $routeParams) {
$scope.slide = SlideApi.get($routeParams.slide_id);
});
!
.controller(‘PresentationSlideCtrl’,
function ($scope, $routeParams, presentation) {
$scope.slide =
presentation.getSlideById($routeParams.slide_id);
});
dependencies 👶
routes views dependencies state bonus
$stateProvider
.state(‘presentation’, {
…
resolve: {
presentation: function ($stateParams, PresentationApi) {
return PresentationApi.get($stateParams.id);
}
}
})
.state(‘presentation.slide’, {
…
resolve: {
slide: function ($stateParams, presentation) {
return presentation.getSlideById($stateParams);
}
}
});
dependencies 👶
routes views dependencies state bonus
“state”?😒
routes views dependencies state bonus
$stateProvider
.state(‘presentation’, {
…
controller:
function ($scope, presentation) {
$scope.presentation = presentation;
},
resolve: {
presentation: function (…) {
PresentationApi.get($stateParams.id);
}
}
})
.state(‘presentation.slide’, {…})
.state(‘presentation.edit’, {…})
.state(‘presentation.show’, {…});
routes views dependencies state bonus
$state.go(‘presentation.slide.show’, { id: 1, slide_id: 2});
!
$state.go(‘user.delete’, {id: 42});
!
$state.go(‘.edit’);
routes views dependencies state bonus
$stateProvider
.state(‘presentation’, {
abstract: true,
…
})
.state(‘presentation.slide’, {
abstract: true,
…
})
.state(‘presentation.edit’, {…})
.state(‘presentation.show’, {…})
.state(‘presentation.slide.edit’, {…})
.state(‘presentation.slide.show’, {…});
bonus features 🍰
routes views dependencies state bonus
$state
.state
abstract: true
…
})
.state
abstract: true
…
})
.state
.state
.state
.state
bonus features 🍰
www.medium.com/@gabescholz
routes views dependencies state bonus
angular.module(‘app’, [‘ui.router’])
!
.config(function ($stateProvider) {
$stateProvider
.state(‘presentation.edit’, {
data: {
permissions: {
admin: true
}
},
…
});
})
!
.run(function ($rootScope, $state) {
var currentUser = $rootScope.currentUser;
!
$rootScope.$on(‘$stateChangeStart’, function (…) {
if (toState.data.permissions.admin && !currentUser.admin) {
event.preventDefault();
$state.go(‘home’);
}
});
});
bonus features 🍰
routes views dependencies state bonus
<a ui-sref=“^.slide({slide_id: (slide.id + 1)})”>
Next Slide
</a>
!
<a ui-sref=“^.slide({slide_id: (slide.id - 1)})”>
Previous Slide
</a>
bonus features 🍰
routes views dependencies state bonus
👏😁thanks

More Related Content

What's hot

What's hot (20)

AngularJS Routing
AngularJS RoutingAngularJS Routing
AngularJS Routing
 
Building an End-to-End AngularJS Application
Building an End-to-End AngularJS ApplicationBuilding an End-to-End AngularJS Application
Building an End-to-End AngularJS Application
 
Dart and AngularDart
Dart and AngularDartDart and AngularDart
Dart and AngularDart
 
Angular meetup - routing and multilingual urls
Angular meetup - routing and multilingual urlsAngular meetup - routing and multilingual urls
Angular meetup - routing and multilingual urls
 
Building a dashboard using AngularJS
Building a dashboard using AngularJSBuilding a dashboard using AngularJS
Building a dashboard using AngularJS
 
Rails3 asset-pipeline
Rails3 asset-pipelineRails3 asset-pipeline
Rails3 asset-pipeline
 
Angular 2.0 Routing and Navigation
Angular 2.0 Routing and NavigationAngular 2.0 Routing and Navigation
Angular 2.0 Routing and Navigation
 
AngularJS Directives
AngularJS DirectivesAngularJS Directives
AngularJS Directives
 
CFUGbe talk about Angular JS
CFUGbe talk about Angular JSCFUGbe talk about Angular JS
CFUGbe talk about Angular JS
 
Optimizing Angular Performance in Enterprise Single Page Apps
Optimizing Angular Performance in Enterprise Single Page AppsOptimizing Angular Performance in Enterprise Single Page Apps
Optimizing Angular Performance in Enterprise Single Page Apps
 
Django Vs Rails
Django Vs RailsDjango Vs Rails
Django Vs Rails
 
Introducing AngularJS
Introducing AngularJSIntroducing AngularJS
Introducing AngularJS
 
Modular and Event-Driven JavaScript
Modular and Event-Driven JavaScriptModular and Event-Driven JavaScript
Modular and Event-Driven JavaScript
 
Get AngularJS Started!
Get AngularJS Started!Get AngularJS Started!
Get AngularJS Started!
 
MEAN - Notes from the field (Full-Stack Development with Javascript)
MEAN - Notes from the field (Full-Stack Development with Javascript)MEAN - Notes from the field (Full-Stack Development with Javascript)
MEAN - Notes from the field (Full-Stack Development with Javascript)
 
Arquitetura de Front-end em Aplicações de Larga Escala
Arquitetura de Front-end em Aplicações de Larga EscalaArquitetura de Front-end em Aplicações de Larga Escala
Arquitetura de Front-end em Aplicações de Larga Escala
 
Understanding angular js $rootscope and $scope
Understanding angular js $rootscope and $scopeUnderstanding angular js $rootscope and $scope
Understanding angular js $rootscope and $scope
 
AngularJS vs. Ember.js vs. Backbone.js
AngularJS vs. Ember.js vs. Backbone.jsAngularJS vs. Ember.js vs. Backbone.js
AngularJS vs. Ember.js vs. Backbone.js
 
Hastening React SSR - Web Performance San Diego
Hastening React SSR - Web Performance San DiegoHastening React SSR - Web Performance San Diego
Hastening React SSR - Web Performance San Diego
 
Steps to create image carousel by using angularjs
Steps to create image carousel by using angularjsSteps to create image carousel by using angularjs
Steps to create image carousel by using angularjs
 

Similar to ui-router and $state

Introducing Rendr: Run your Backbone.js apps on the client and server
Introducing Rendr: Run your Backbone.js apps on the client and serverIntroducing Rendr: Run your Backbone.js apps on the client and server
Introducing Rendr: Run your Backbone.js apps on the client and server
Spike Brehm
 

Similar to ui-router and $state (20)

Using and reusing CakePHP plugins
Using and reusing CakePHP pluginsUsing and reusing CakePHP plugins
Using and reusing CakePHP plugins
 
Introducing Rendr: Run your Backbone.js apps on the client and server
Introducing Rendr: Run your Backbone.js apps on the client and serverIntroducing Rendr: Run your Backbone.js apps on the client and server
Introducing Rendr: Run your Backbone.js apps on the client and server
 
Angular Workshop_Sarajevo2
Angular Workshop_Sarajevo2Angular Workshop_Sarajevo2
Angular Workshop_Sarajevo2
 
Design strategies for AngularJS
Design strategies for AngularJSDesign strategies for AngularJS
Design strategies for AngularJS
 
Advanced RESTful Rails
Advanced RESTful RailsAdvanced RESTful Rails
Advanced RESTful Rails
 
Advanced RESTful Rails
Advanced RESTful RailsAdvanced RESTful Rails
Advanced RESTful Rails
 
State Machines to State of the Art
State Machines to State of the ArtState Machines to State of the Art
State Machines to State of the Art
 
Laravel Routing and Query Building
Laravel   Routing and Query BuildingLaravel   Routing and Query Building
Laravel Routing and Query Building
 
Angular the Good Parts
Angular the Good PartsAngular the Good Parts
Angular the Good Parts
 
Opencast Admin UI - Introduction to developing using AngularJS
Opencast Admin UI - Introduction to developing using AngularJSOpencast Admin UI - Introduction to developing using AngularJS
Opencast Admin UI - Introduction to developing using AngularJS
 
Angular in the Enterprise
Angular in the EnterpriseAngular in the Enterprise
Angular in the Enterprise
 
Angular directive filter and routing
Angular directive filter and routingAngular directive filter and routing
Angular directive filter and routing
 
Angular server side rendering - Strategies & Technics
Angular server side rendering - Strategies & Technics Angular server side rendering - Strategies & Technics
Angular server side rendering - Strategies & Technics
 
Routing And Navigation
Routing And NavigationRouting And Navigation
Routing And Navigation
 
Introduction to angular js
Introduction to angular jsIntroduction to angular js
Introduction to angular js
 
Codeigniter : Custom Routing - Manipulate Uri
Codeigniter : Custom Routing - Manipulate UriCodeigniter : Custom Routing - Manipulate Uri
Codeigniter : Custom Routing - Manipulate Uri
 
Good karma: UX Patterns and Unit Testing in Angular with Karma
Good karma: UX Patterns and Unit Testing in Angular with KarmaGood karma: UX Patterns and Unit Testing in Angular with Karma
Good karma: UX Patterns and Unit Testing in Angular with Karma
 
Building Single Page Application (SPA) with Symfony2 and AngularJS
Building Single Page Application (SPA) with Symfony2 and AngularJSBuilding Single Page Application (SPA) with Symfony2 and AngularJS
Building Single Page Application (SPA) with Symfony2 and AngularJS
 
Magento Live Australia 2016: Request Flow
Magento Live Australia 2016: Request FlowMagento Live Australia 2016: Request Flow
Magento Live Australia 2016: Request Flow
 
Mock Servers - Fake All the Things!
Mock Servers - Fake All the Things!Mock Servers - Fake All the Things!
Mock Servers - Fake All the Things!
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Recently uploaded (20)

Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 

ui-router and $state