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

Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
UXDXConf
 

Recently uploaded (20)

WSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptxWSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptx
 
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone KomSalesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
 
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
 
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
 
PLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. StartupsPLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. Startups
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM Performance
 
Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
 
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
 
The Metaverse: Are We There Yet?
The  Metaverse:    Are   We  There  Yet?The  Metaverse:    Are   We  There  Yet?
The Metaverse: Are We There Yet?
 
Top 10 Symfony Development Companies 2024
Top 10 Symfony Development Companies 2024Top 10 Symfony Development Companies 2024
Top 10 Symfony Development Companies 2024
 
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCustom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
 
Optimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through ObservabilityOptimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through Observability
 
What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024
 
Oauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoftOauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoft
 
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
 
Enterprise Knowledge Graphs - Data Summit 2024
Enterprise Knowledge Graphs - Data Summit 2024Enterprise Knowledge Graphs - Data Summit 2024
Enterprise Knowledge Graphs - Data Summit 2024
 
Google I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGoogle I/O Extended 2024 Warsaw
Google I/O Extended 2024 Warsaw
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
 

ui-router and $state