SlideShare une entreprise Scribd logo
1  sur  38
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

Tendances (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
 

Similaire à 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
 

Similaire à 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!
 

Dernier

+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@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Dernier (20)

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
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
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
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
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
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
+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...
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 

ui-router and $state