SlideShare a Scribd company logo
Angular Promises and Advanced Routing
Angular Promises and Advanced Routing
A represents a task that will finish in the future; 
A has three methods: 
then() 
catch() 
finally() 
A will be or
var a = 10; 
window.setTimeout(function(){ 
a += 10; 
}, 1000); 
console.log(a); // Will output 10;
setTimeout() / setInterval(); 
XmlHttpRequest; 
Element Creation; 
Web Workers; 
Generators;
* Immutable
Angular Promises and Advanced Routing
Angular uses promises for everything that is async: 
$timeout; 
$http & response interceptors; 
$resource; 
$routeProvider.when(); 
much more.
Implemented using $q service 
function createPromise(resolve){ 
defered = $q.defer(); 
window.setTimeout(function(){ 
if (resolve) defered.resolve(10); 
else defered.reject("You provided a false value"); 
}, 1000); 
return defered.promise; 
} 
var promise = createPromise(true); 
promise.then(function(value){ 
console.log(value); 
}); 
console.log("The value will be shown after this.");
Result of 
var promise = createPromise(true); 
promise 
.then(function(value){ 
console.log(value) //10 
return value + 10; 
}, errorFn, notifyFn) 
.then(function(value){ 
console.log(value) //20 
return $q.reject(20); 
}, errorFn, notifyFn) 
.catch(errorFn) 
.finally(callbackFn);
ui-router
Angular Promises and Advanced Routing
Angular Promises and Advanced Routing
A within an app 
hierarchy 
Names are 
Navigate by 
views (ui-view) 
Populate 
populates 'parts' 
A within your app 
hierarchy 
Names are 
Navigate by 
view (ng-view) 
Populate 
populate 
'parts'
<body> 
<ul> 
<li><a ui-sref="home">Home</a></li> 
<li><a ui-sref="about">About</a></li> 
</ul> 
<section ui-view></section> 
</body> 
angular 
.module("myApp", []) 
.config(['$stateProvider', function($stateProvider){ 
$stateProvider 
.state('home', { 
template: '<h1>Home</h1>' 
}) 
.state('about', { 
template: '<h1>About</h1>' 
}); 
}])
$stateProvider 
.state('stateName', { 
template: 'String Html content', 
templateUrl: 'String URL', 
templateProvider: function(){}, // Function, must return HTML String 
controller: 'Function or name as String' 
controllerProvider: function(){}, // Function, must return controller function or name as String 
resolve: {} // A map of dependencies wich shoul be resolved and injected into controller 
url: 'A url with optional params', 
params: [], //Array of parameter names or regular expressions 
views: { 
//Object to define multiple views 
'nameOfView': { 
template: '', 
controller: '' 
} 
}, 
abstract: true //An abstract state will never be directly activated, but can provide inherited data: {}, 
onEnter: function(){}, 
onExit: function(){}, 
reloadOnSearch: true 
})
The resolve property is a map object: 
: name of the dependency 
: 
: alias for a service 
: 
Return value is treated as the dependency. 
If the result is a promise, it's resolved before 
the controller is instatiated.
$stateProvider 
.state('stateName', { 
resolve:{ 
simpleObj: function() { 
return {value: 'some string'} 
}, 
promiseObj: ['$http', function($http) { 
//$http returns a promise 
return $http({method: 'GET', url: '/someUrl'}); 
}] 
}, 
controller: ['$scope', 'simpleObj', 'promiseObj', function($scope, simpleObj, promiseObj){ 
$scope.simple = simpleObj.value; 
// You can be sure that promiseObj is ready to use! 
$scope.items = promiseObj.items; 
$scope.items = promiseObj2.items; 
}] 
})
There are three main ways to activate a state: 
1. Call $state.go(); 
2. Click a link containing the ui-sref directive; 
3. Navigate to the url associated with the state.
myApp.controller('contactCtrl', ['$scope', '$state', 
function($scope, $state){ 
$scope.goToDetails = function(){ 
$state.go('contact.details', {id: selectedId}); 
} 
} 
Params: 
1. state name 
2. state params ( ) 
3. options ( )
You can navigate relative to current state 
by using special characters: 
1. is up; 
2. is down.
Go to sibling - $state.go('^.1')
Generate anchors for state references 
<a ui-sref="contacts.detail({ id: 3 })"></a> 
<!-- Can Generate --> 
<a ui-sref="contacts.detail({ id: 3 })" href="#/contacts/3"></a> 
Can use relative paths and can have options 
<a ui-sref="^" ui-sref-opts="{location : false}">Go up</a>
$stateProvider 
.state("home", { ... }); 
.state("contacts", { ... }); 
.state("contacts.detail", { ... }); 
.state("contacts.detail.edit", { ... }); 
The dot in the state names auto-denotes parental 
hierarchy. 
It knows that is a of .
<!-- index.html --> 
<body> 
<div ui-view></div> 
</body> 
<!-- page.html --> 
<h1>My Contacts</h1> 
<div ui-view></div> 
<!-- page.list.html --> 
<ul> 
<li ng-repeat="item in list"> 
<a>{{item.name}}</a> 
</li> 
</ul> 
angular 
.module('myapp', ["ui.router"]) 
.config(function($stateProvider, $urlRouterProvider 
$urlRouterProvider.otherwise("/list") 
$stateProvider 
.state('page', { 
abstract: true, 
templateUrl: 'page.html', 
resolve: { 
list: function($http){ 
return $http({method: 'GET', url: '/someUrl' 
} 
} 
}) 
.state('page.list', { 
url: '/list', 
controller: function($scope, list){ 
$scope.list= list; 
} 
templateUrl: 'page.list.html' 
}); 
});
Scope inherits from parent 
properties 
methods 
Child states inherit from their parents: 
resolved dependencies 
custom data
.state('parent', { 
resolve:{ 
resA: function(){ 
return {'value': 'A'}; 
} 
}, 
controller: function($scope, resA){ 
$scope.resA = resA.value; 
} 
}) 
.state('parent.child', { 
resolve:{ 
resB: function(resA){ 
return {'value': resA.value + 'B'}; 
} 
}, 
controller: function($scope, resA, resB){ 
$scope.resA2 = resA.value; 
$scope.resB = resB.value; 
}
<body> 
<div ui-view="filters"></div> 
<div ui-view="tabledata"></div> 
<div ui-view="graph"></div> 
</body> 
$stateProvider 
.state('report',{ 
url: '', 
resolve: {}, 
views: { 
'filters': { 
templateUrl: 'report-filters.html', 
controller: function($scope){} 
}, 
'tabledata': { 
templateUrl: 'report-table.html', 
controller: function($scope){} 
}, 
'graph': { 
templateUrl: 'report-graph.html', 
controller: function($scope){} 
}, 
} 
})
Can use relative or absolute naming 
(always parent) 
filters - 'filters' view in parent template 
- unnamed view in parent template 
(uses @ symbol) 
- 'filters' view in 'report' state's template 
- 'filters' view in index.html. 
- unnamed view in 'report' state's template
$stateProvider 
.state('page', { 
url: "/page", 
templateUrl: 'page.html' 
}) 
.state('page.item', { 
url: "/{id}?filter&anotherParam", 
templateUrl: 'page-item.html', 
controller: function ($stateParams) { 
console.log($stateParams.id) 
} 
}) 
.state('page.item.details', { 
url: "/details", 
templateUrl: 'page-item-details.html', 
}) 
page.item's url becomes: 
page.item.details url becomes:
Plunker link
Angular API Reference 
Tim Kindberg slides 
uiRouter guide

More Related Content

PPTX
UI-Router
Loc Nguyen
 
PDF
Angular js routing options
Nir Kaufman
 
PPTX
AngularJS Routing
Eyal Vardi
 
PPTX
Dart and AngularDart
Loc Nguyen
 
PDF
Angular JS Routing
kennystoltz
 
PPTX
AngularJS Directives
Eyal Vardi
 
PDF
ui-router and $state
garbles
 
PPTX
AngularJS - $http & $resource Services
Eyal Vardi
 
UI-Router
Loc Nguyen
 
Angular js routing options
Nir Kaufman
 
AngularJS Routing
Eyal Vardi
 
Dart and AngularDart
Loc Nguyen
 
Angular JS Routing
kennystoltz
 
AngularJS Directives
Eyal Vardi
 
ui-router and $state
garbles
 
AngularJS - $http & $resource Services
Eyal Vardi
 

What's hot (20)

PPTX
Dive into AngularJS Routing
Egor Miasnikov
 
PDF
Angular promises and http
Alexe Bogdan
 
PPTX
Angular 2.0 Routing and Navigation
Eyal Vardi
 
PPTX
AngularJS Services
Eyal Vardi
 
PPTX
Introducing AngularJS
Loc Nguyen
 
PPTX
Building an End-to-End AngularJS Application
Dan Wahlin
 
PDF
Ui router
Marilyn Waldman
 
PPTX
AngularJS $Provide Service
Eyal Vardi
 
PPTX
Upgrading from Angular 1.x to Angular 2.x
Eyal Vardi
 
PDF
Angular JS blog tutorial
Claude Tech
 
PPTX
Angular 1.x vs. Angular 2.x
Eyal Vardi
 
PPTX
AngularJS Internal
Eyal Vardi
 
PPTX
AngularJS Compile Process
Eyal Vardi
 
PPTX
Routing And Navigation
Eyal Vardi
 
PDF
Workshop 12: AngularJS Parte I
Visual Engineering
 
PPTX
AngularJS for Java Developers
Loc Nguyen
 
PPTX
Modules and injector
Eyal Vardi
 
PDF
Workshop 17: EmberJS parte II
Visual Engineering
 
PDF
Sane Async Patterns
TrevorBurnham
 
PDF
Get AngularJS Started!
Dzmitry Ivashutsin
 
Dive into AngularJS Routing
Egor Miasnikov
 
Angular promises and http
Alexe Bogdan
 
Angular 2.0 Routing and Navigation
Eyal Vardi
 
AngularJS Services
Eyal Vardi
 
Introducing AngularJS
Loc Nguyen
 
Building an End-to-End AngularJS Application
Dan Wahlin
 
Ui router
Marilyn Waldman
 
AngularJS $Provide Service
Eyal Vardi
 
Upgrading from Angular 1.x to Angular 2.x
Eyal Vardi
 
Angular JS blog tutorial
Claude Tech
 
Angular 1.x vs. Angular 2.x
Eyal Vardi
 
AngularJS Internal
Eyal Vardi
 
AngularJS Compile Process
Eyal Vardi
 
Routing And Navigation
Eyal Vardi
 
Workshop 12: AngularJS Parte I
Visual Engineering
 
AngularJS for Java Developers
Loc Nguyen
 
Modules and injector
Eyal Vardi
 
Workshop 17: EmberJS parte II
Visual Engineering
 
Sane Async Patterns
TrevorBurnham
 
Get AngularJS Started!
Dzmitry Ivashutsin
 
Ad

Viewers also liked (18)

PDF
AngularJS Best Practices
Betclic Everest Group Tech Team
 
PPT
Coffee@DBG - Exploring Angular JS
Deepu S Nath
 
PDF
Intro to UI-Router/TypeScript
Jamal Sinclair O'Garro
 
PDF
Architecture, Auth, and Routing with uiRouter
Christopher Caplinger
 
PPTX
Refactoring
Martin Salias
 
PPTX
Refactoring Golf
Angel Nuñez
 
PDF
Angular2 & ngrx/store: Game of States
Oren Farhi
 
PPTX
Basics of angular directive (Part - 1)
Vijay Kani
 
PPTX
Redux in Angular2 for jsbe
Brecht Billiet
 
PDF
AngularJS Custom Directives
yprodev
 
PPTX
AngularJS custom directive
Nascenia IT
 
PPTX
Building AngularJS Custom Directives
Dan Wahlin
 
PDF
redux and angular - up and running
Nir Kaufman
 
PDF
Angular redux
Nir Kaufman
 
PDF
Functional Reactive Angular 2
Tomasz Bak
 
PDF
Redux with angular 2 - workshop 2016
Nir Kaufman
 
PDF
Refactoring - An Introduction
Giorgio Vespucci
 
PPT
Refactoring Tips by Martin Fowler
Igor Crvenov
 
AngularJS Best Practices
Betclic Everest Group Tech Team
 
Coffee@DBG - Exploring Angular JS
Deepu S Nath
 
Intro to UI-Router/TypeScript
Jamal Sinclair O'Garro
 
Architecture, Auth, and Routing with uiRouter
Christopher Caplinger
 
Refactoring
Martin Salias
 
Refactoring Golf
Angel Nuñez
 
Angular2 & ngrx/store: Game of States
Oren Farhi
 
Basics of angular directive (Part - 1)
Vijay Kani
 
Redux in Angular2 for jsbe
Brecht Billiet
 
AngularJS Custom Directives
yprodev
 
AngularJS custom directive
Nascenia IT
 
Building AngularJS Custom Directives
Dan Wahlin
 
redux and angular - up and running
Nir Kaufman
 
Angular redux
Nir Kaufman
 
Functional Reactive Angular 2
Tomasz Bak
 
Redux with angular 2 - workshop 2016
Nir Kaufman
 
Refactoring - An Introduction
Giorgio Vespucci
 
Refactoring Tips by Martin Fowler
Igor Crvenov
 
Ad

Similar to Angular Promises and Advanced Routing (20)

PPTX
Angular Workshop_Sarajevo2
Christoffer Noring
 
PPTX
Practical AngularJS
Wei Ru
 
PPTX
AngularJS: what is underneath the hood
DA-14
 
PPTX
Introduction to Angular JS
Santhosh Kumar Srinivasan
 
PDF
Workshop 13: AngularJS Parte II
Visual Engineering
 
ODP
AngularJs Crash Course
Keith Bloomfield
 
PPTX
Angular js 1.0-fundamentals
Venkatesh Narayanan
 
PPTX
AngularJS.part1
Andrey Kolodnitsky
 
PPTX
Single Page Applications Workshop Part II: Single Page Applications using Ang...
Jalal Mostafa
 
PDF
Angular resolver tutorial
Katy Slemon
 
PDF
Resources and relationships at front-end
Wingify Engineering
 
PPTX
Basics of AngularJS
Filip Janevski
 
PPTX
Angular - a real world case study
dwcarter74
 
PPTX
AngularJS - a radically different way of building Single Page Apps
jivkopetiov
 
PPTX
Intro to AngularJs
SolTech, Inc.
 
PDF
JS and patterns
David Rodenas
 
PDF
JavaScript Abstraction
☆ Milan Adamovsky ☆
 
PPTX
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
Learnimtactics
 
PDF
AngularJS Basics and Best Practices - CC FE &UX
JWORKS powered by Ordina
 
PDF
AngularJS Workshop
Gianluca Cacace
 
Angular Workshop_Sarajevo2
Christoffer Noring
 
Practical AngularJS
Wei Ru
 
AngularJS: what is underneath the hood
DA-14
 
Introduction to Angular JS
Santhosh Kumar Srinivasan
 
Workshop 13: AngularJS Parte II
Visual Engineering
 
AngularJs Crash Course
Keith Bloomfield
 
Angular js 1.0-fundamentals
Venkatesh Narayanan
 
AngularJS.part1
Andrey Kolodnitsky
 
Single Page Applications Workshop Part II: Single Page Applications using Ang...
Jalal Mostafa
 
Angular resolver tutorial
Katy Slemon
 
Resources and relationships at front-end
Wingify Engineering
 
Basics of AngularJS
Filip Janevski
 
Angular - a real world case study
dwcarter74
 
AngularJS - a radically different way of building Single Page Apps
jivkopetiov
 
Intro to AngularJs
SolTech, Inc.
 
JS and patterns
David Rodenas
 
JavaScript Abstraction
☆ Milan Adamovsky ☆
 
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
Learnimtactics
 
AngularJS Basics and Best Practices - CC FE &UX
JWORKS powered by Ordina
 
AngularJS Workshop
Gianluca Cacace
 

More from Alexe Bogdan (7)

PDF
Dependency Injection pattern in Angular
Alexe Bogdan
 
PDF
Client Side MVC & Angular
Alexe Bogdan
 
PDF
HTML & JavaScript Introduction
Alexe Bogdan
 
PDF
Angular custom directives
Alexe Bogdan
 
PDF
Angular server-side communication
Alexe Bogdan
 
PDF
AngularJS - dependency injection
Alexe Bogdan
 
PDF
AngularJS - introduction & how it works?
Alexe Bogdan
 
Dependency Injection pattern in Angular
Alexe Bogdan
 
Client Side MVC & Angular
Alexe Bogdan
 
HTML & JavaScript Introduction
Alexe Bogdan
 
Angular custom directives
Alexe Bogdan
 
Angular server-side communication
Alexe Bogdan
 
AngularJS - dependency injection
Alexe Bogdan
 
AngularJS - introduction & how it works?
Alexe Bogdan
 

Recently uploaded (20)

PDF
DNSSEC Made Easy, presented at PHNOG 2025
APNIC
 
PPTX
Microsoft PowerPoint Student PPT slides.pptx
Garleys Putin
 
PDF
Slides PDF The Workd Game (s) Eco Economic Epochs.pdf
Steven McGee
 
PPTX
B2B_Ecommerce_Internship_Simranpreet.pptx
LipakshiJindal
 
PPTX
ENCOR_Chapter_11 - ‌BGP implementation.pptx
nshg93
 
PPTX
EthicalHack{aksdladlsfsamnookfmnakoasjd}.pptx
dagarabull
 
PDF
Centralized Business Email Management_ How Admin Controls Boost Efficiency & ...
XgenPlus Technologies
 
PDF
Elements Of Poetry PowerPoint With Sources
PrincessKate16
 
PDF
LABUAN4D EXCLUSIVE SERVER STAR GAMING ASIA NO.1
LABUAN 4D
 
PPTX
Slides, PPTX World Game (s) Eco Economic Epochs.pptx
Steven McGee
 
PPTX
谢尔丹学院毕业证购买|Sheridan文凭不见了怎么办谢尔丹学院成绩单
mookxk3
 
PDF
Data Protection & Resilience in Focus.pdf
AmyPoblete3
 
PDF
Project English Paja Jara Alejandro.jpdf
AlejandroAlonsoPajaJ
 
PDF
5g is Reshaping the Competitive Landscape
Stellarix
 
PPTX
Black Yellow Modern Minimalist Elegant Presentation.pptx
nothisispatrickduhh
 
PPTX
PPT_M4.3_WORKING WITH SLIDES APPLIED.pptx
MCEAMONVILLAVER
 
PPTX
Parallel & Concurrent ...
yashpavasiya892
 
PDF
Triggering QUIC, presented by Geoff Huston at IETF 123
APNIC
 
PPT
Transformaciones de las funciones elementales.ppt
rirosel211
 
PDF
KIPER4D situs Exclusive Game dari server Star Gaming Asia
hokimamad0
 
DNSSEC Made Easy, presented at PHNOG 2025
APNIC
 
Microsoft PowerPoint Student PPT slides.pptx
Garleys Putin
 
Slides PDF The Workd Game (s) Eco Economic Epochs.pdf
Steven McGee
 
B2B_Ecommerce_Internship_Simranpreet.pptx
LipakshiJindal
 
ENCOR_Chapter_11 - ‌BGP implementation.pptx
nshg93
 
EthicalHack{aksdladlsfsamnookfmnakoasjd}.pptx
dagarabull
 
Centralized Business Email Management_ How Admin Controls Boost Efficiency & ...
XgenPlus Technologies
 
Elements Of Poetry PowerPoint With Sources
PrincessKate16
 
LABUAN4D EXCLUSIVE SERVER STAR GAMING ASIA NO.1
LABUAN 4D
 
Slides, PPTX World Game (s) Eco Economic Epochs.pptx
Steven McGee
 
谢尔丹学院毕业证购买|Sheridan文凭不见了怎么办谢尔丹学院成绩单
mookxk3
 
Data Protection & Resilience in Focus.pdf
AmyPoblete3
 
Project English Paja Jara Alejandro.jpdf
AlejandroAlonsoPajaJ
 
5g is Reshaping the Competitive Landscape
Stellarix
 
Black Yellow Modern Minimalist Elegant Presentation.pptx
nothisispatrickduhh
 
PPT_M4.3_WORKING WITH SLIDES APPLIED.pptx
MCEAMONVILLAVER
 
Parallel & Concurrent ...
yashpavasiya892
 
Triggering QUIC, presented by Geoff Huston at IETF 123
APNIC
 
Transformaciones de las funciones elementales.ppt
rirosel211
 
KIPER4D situs Exclusive Game dari server Star Gaming Asia
hokimamad0
 

Angular Promises and Advanced Routing

  • 3. A represents a task that will finish in the future; A has three methods: then() catch() finally() A will be or
  • 4. var a = 10; window.setTimeout(function(){ a += 10; }, 1000); console.log(a); // Will output 10;
  • 5. setTimeout() / setInterval(); XmlHttpRequest; Element Creation; Web Workers; Generators;
  • 8. Angular uses promises for everything that is async: $timeout; $http & response interceptors; $resource; $routeProvider.when(); much more.
  • 9. Implemented using $q service function createPromise(resolve){ defered = $q.defer(); window.setTimeout(function(){ if (resolve) defered.resolve(10); else defered.reject("You provided a false value"); }, 1000); return defered.promise; } var promise = createPromise(true); promise.then(function(value){ console.log(value); }); console.log("The value will be shown after this.");
  • 10. Result of var promise = createPromise(true); promise .then(function(value){ console.log(value) //10 return value + 10; }, errorFn, notifyFn) .then(function(value){ console.log(value) //20 return $q.reject(20); }, errorFn, notifyFn) .catch(errorFn) .finally(callbackFn);
  • 14. A within an app hierarchy Names are Navigate by views (ui-view) Populate populates 'parts' A within your app hierarchy Names are Navigate by view (ng-view) Populate populate 'parts'
  • 15. <body> <ul> <li><a ui-sref="home">Home</a></li> <li><a ui-sref="about">About</a></li> </ul> <section ui-view></section> </body> angular .module("myApp", []) .config(['$stateProvider', function($stateProvider){ $stateProvider .state('home', { template: '<h1>Home</h1>' }) .state('about', { template: '<h1>About</h1>' }); }])
  • 16. $stateProvider .state('stateName', { template: 'String Html content', templateUrl: 'String URL', templateProvider: function(){}, // Function, must return HTML String controller: 'Function or name as String' controllerProvider: function(){}, // Function, must return controller function or name as String resolve: {} // A map of dependencies wich shoul be resolved and injected into controller url: 'A url with optional params', params: [], //Array of parameter names or regular expressions views: { //Object to define multiple views 'nameOfView': { template: '', controller: '' } }, abstract: true //An abstract state will never be directly activated, but can provide inherited data: {}, onEnter: function(){}, onExit: function(){}, reloadOnSearch: true })
  • 17. The resolve property is a map object: : name of the dependency : : alias for a service : Return value is treated as the dependency. If the result is a promise, it's resolved before the controller is instatiated.
  • 18. $stateProvider .state('stateName', { resolve:{ simpleObj: function() { return {value: 'some string'} }, promiseObj: ['$http', function($http) { //$http returns a promise return $http({method: 'GET', url: '/someUrl'}); }] }, controller: ['$scope', 'simpleObj', 'promiseObj', function($scope, simpleObj, promiseObj){ $scope.simple = simpleObj.value; // You can be sure that promiseObj is ready to use! $scope.items = promiseObj.items; $scope.items = promiseObj2.items; }] })
  • 19. There are three main ways to activate a state: 1. Call $state.go(); 2. Click a link containing the ui-sref directive; 3. Navigate to the url associated with the state.
  • 20. myApp.controller('contactCtrl', ['$scope', '$state', function($scope, $state){ $scope.goToDetails = function(){ $state.go('contact.details', {id: selectedId}); } } Params: 1. state name 2. state params ( ) 3. options ( )
  • 21. You can navigate relative to current state by using special characters: 1. is up; 2. is down.
  • 22. Go to sibling - $state.go('^.1')
  • 23. Generate anchors for state references <a ui-sref="contacts.detail({ id: 3 })"></a> <!-- Can Generate --> <a ui-sref="contacts.detail({ id: 3 })" href="#/contacts/3"></a> Can use relative paths and can have options <a ui-sref="^" ui-sref-opts="{location : false}">Go up</a>
  • 24. $stateProvider .state("home", { ... }); .state("contacts", { ... }); .state("contacts.detail", { ... }); .state("contacts.detail.edit", { ... }); The dot in the state names auto-denotes parental hierarchy. It knows that is a of .
  • 25. <!-- index.html --> <body> <div ui-view></div> </body> <!-- page.html --> <h1>My Contacts</h1> <div ui-view></div> <!-- page.list.html --> <ul> <li ng-repeat="item in list"> <a>{{item.name}}</a> </li> </ul> angular .module('myapp', ["ui.router"]) .config(function($stateProvider, $urlRouterProvider $urlRouterProvider.otherwise("/list") $stateProvider .state('page', { abstract: true, templateUrl: 'page.html', resolve: { list: function($http){ return $http({method: 'GET', url: '/someUrl' } } }) .state('page.list', { url: '/list', controller: function($scope, list){ $scope.list= list; } templateUrl: 'page.list.html' }); });
  • 26. Scope inherits from parent properties methods Child states inherit from their parents: resolved dependencies custom data
  • 27. .state('parent', { resolve:{ resA: function(){ return {'value': 'A'}; } }, controller: function($scope, resA){ $scope.resA = resA.value; } }) .state('parent.child', { resolve:{ resB: function(resA){ return {'value': resA.value + 'B'}; } }, controller: function($scope, resA, resB){ $scope.resA2 = resA.value; $scope.resB = resB.value; }
  • 28. <body> <div ui-view="filters"></div> <div ui-view="tabledata"></div> <div ui-view="graph"></div> </body> $stateProvider .state('report',{ url: '', resolve: {}, views: { 'filters': { templateUrl: 'report-filters.html', controller: function($scope){} }, 'tabledata': { templateUrl: 'report-table.html', controller: function($scope){} }, 'graph': { templateUrl: 'report-graph.html', controller: function($scope){} }, } })
  • 29. Can use relative or absolute naming (always parent) filters - 'filters' view in parent template - unnamed view in parent template (uses @ symbol) - 'filters' view in 'report' state's template - 'filters' view in index.html. - unnamed view in 'report' state's template
  • 30. $stateProvider .state('page', { url: "/page", templateUrl: 'page.html' }) .state('page.item', { url: "/{id}?filter&anotherParam", templateUrl: 'page-item.html', controller: function ($stateParams) { console.log($stateParams.id) } }) .state('page.item.details', { url: "/details", templateUrl: 'page-item-details.html', }) page.item's url becomes: page.item.details url becomes:
  • 32. Angular API Reference Tim Kindberg slides uiRouter guide