SlideShare uma empresa Scribd logo
1 de 31
Baixar para ler offline
Exploring routing options
Routing is mandatory.
For an angular app, we can choose between
the official ngRoute, or the popular ui-router.
In this talk i will introduce you with both of
them so you can choose what's fits your
needs.
nirkaufman@gmail.com
Spoiler!
In the end of this talk you will probably choose
to use ui-router for your project.
nirkaufman@gmail.com
What's the plan?
- Exploring the modules core features and
API.
- Asking questions & getting answers
but most important..
nirkaufman@gmail.com
Seeing it in Action!
Walking through a working demo project.
find the github link in the last slide
nirkaufman@gmail.com
ngRoute
Used to be baked into Angular core,
separated into it`s own module in version 1.2.
Provides a lot of functionality we expected
from a routing system.
nirkaufman@gmail.com
Installation
nirkaufman@gmail.com
use bower (or download manually) the angular-route.js file.
Make sure to load the module after angular.
specify ngRoute as a dependency for your module
$ bower install angular-route
<script src="angular.js"> </script>
<script src="angular-route.js"> </script>
angular.module('app', ['ngRoute']);
Simple route
nirkaufman@gmail.com
angular.module('app', ['ngRoute'])
.config(function ($routeProvider) {
$routeProvider
.when('/home',{ templateUrl: ‘views/home.html’ })
.when('/user',{ templateUrl: ‘views/user.html’ })
.otherwise({ template: “<div>NOT FOUND!</div>” })
In the config section of our module, we use the $routeProvider to map url`s to the
desired views. the most basic route should include an HTML template to render.
Navigate & Display Templates
nirkaufman@gmail.com
<ng-view onload=”fn()”></ng-view>
Our template will be rendered between the ng-view tags. this directive will create a
new child scope for the template. we can pass a function to the onload attribute.
ngRouter Support only one ngView per Application!
We can display the requested from html using <a> tags, or from javaScript using the
$location service:
function UserController ($location) {
$location.path(‘/admin’)}
Controllers & Parameters
nirkaufman@gmail.com
we can assign a controller to the view, and access the route parameters by injecting
the $routeParams service to our controller
.when('/user/:id,{
templateUrl: ‘views/user.html’,
controller: ‘UserController’ })
function UserController ($routeParams) {
$routeParams.id // 5234 }
http://www.yourApp.com/user/5234
Attaching custom data
nirkaufman@gmail.com
We can extend the route object to include extra data that we might need.
.when('/admin,{
templateUrl: ‘views/user.html’,
controller: ‘UserController’,
permissions: {level : ‘admin’, key: ‘23f’}
})
...
function UserController ($route) {
permissions = $route.current.permissions
}
Using resolve
nirkaufman@gmail.com
We can define a map of factory functions that will be resolved, and injected to our
controller.. if any of them is a promise, the route will hold until the resolved. it can be
used to load additional resources on demand or fetching data from a remote server.
.when('/admin,{
templateUrl: ‘views/user.html’,
controller: ‘UserController’,
resolve: {data: function() {return “info”} }
})
function UserController (data) {...}
Route LIfe cycle & events
nirkaufman@gmail.com
Url
Requested
$routeChangeStart
$routeChangeError
$routeChangeSuccess
ng-view kicks in$viewContentLoaded
onload function
ngView in Action
nirkaufman@gmail.com
$routeChangeSucsses
broadcasted
create New Scope
destroy last scope,
remove the last template
Link the new Template
with the new Scope
Link the controller
Emit
$viewContentLoaded
run the onload function
Things we didn't cover
nirkaufman@gmail.com
● $locationProvider - configure how the application deep linking paths are
stored (enable HTML5 mode, and define an hash prefix)
● $location - Represents the URL object as a set of methods (protocol,
host, port, path, search, hash)
● $route.reload() - a method that reloads the current view be causing the
ng-view directive to create new child scope and re-instantiate the
controller
● ngView autoscroll - calling to the $anchorScroll service
https://docs.angularjs.org/api/ngRoute/service/$route
UI Router
UI Router is a routing system for AngularJS
that based around the concept of states which
may optionally have routes, as well as other
behaviours.
nirkaufman@gmail.com
Define: state.
❏ a ‘place’ in the application in terms of UI
and navigation.
❏ a state describes what the UI looks like and
does at that place.
nirkaufman@gmail.com
Installation
use bower (or download manually) the angular-ui-router.js file.
Make sure to load the module after angular.
specify ui.router as a dependency for your module
nirkaufman@gmail.com
$ bower install angular-ui-router
<script src="angular.js"> </script>
<script src="angular-ui-router.js"> </script>
angular.module('app', ['ui.router']);
Simple State
nirkaufman@gmail.com
angular.module('app', ['ngRoute'])
.config(function ($stateProvider) {
$stateProvider
.state('home',{
url: ‘/home.html’,
templateUrl: ‘views/home.html’
})
In the config section of our module, we use the $stateProvider to define a state
object and give it a name
Navigate & Display Templates
nirkaufman@gmail.com
<ui-view> </ui-view>
Our template will be rendered between the ui-view tags.
ngRouter Support multiply ui-views per application!
We can display the requested from html using <a ui-sref=’stateName’> tags with
the or using the $state service method:
function UserController ($state) {
$state.go(‘home’)}
Controllers & Parameters
nirkaufman@gmail.com
just like $routeProvider, we can assign a controller to the state, and access the state
parameters by injecting the $stateParams service to our controller
.state('user,{
url: ‘/user/:id’
templateUrl: ‘views/user.html’,
controller: ‘UserController’ })
function UserController ($stateParams) {
$stateParams.id // 5234 }
http://www.yourApp.com/user/5234
Attaching custom data
nirkaufman@gmail.com
Another powerful feature is the ability to display different views in parallel:
.state('home',{
controller: ‘HomeController’
data : {
level: ‘admin’
}}
...
function HomeController ($state) {
data = $state.current.data
}
Nested Views
nirkaufman@gmail.com
One of the powerful features of ui router is the ability to define nested views:
$stateProvider
.state('home',{
url: ‘/home’,
templateUrl: ‘views/home.html’
})
.state('home.subsection,{
url: ‘/subsection’,
templateUrl: ‘views/section.html’
})
Named Views
nirkaufman@gmail.com
Another powerful feature is the ability to display different views in parallel:
$stateProvider
.state('home',{
views: {
"": { template: "<h1>HELLO!</h1>" },
"sidebar": { template: "<sidebar/>" },
"footer": { template: "<data_thing/>" }}...
index.html:
<div ui-view></div>
<div ui-view="sidebar"></div>
<div ui-view="footer"></div>
State Callbacks
nirkaufman@gmail.com
We can optionally define two callbacks to the state object, that will be fired when a
state beacom active or inactive, the callbacks can access to the resolved attributes
.state('home',{
resolve : { user: function () {...} }
onEnter : function (user) {},
onExit : function (user) {}
}
State LIfe cycle & events
nirkaufman@gmail.com
state
Requested
$stateChangeStart
$stateChangeError
$stateChangeSucsses
ui-view kicks in
$viewContentLoaded onload function Done!
$stateNotFound
Things we didn't cover
nirkaufman@gmail.com
● $state API - The $state service contain more methods beside go that we
take advantage of
● $templateFactory- a utility for defining templates in different ways
● state inheritance - child states inherited the resolves from their parent
state
● abstract - we can define an abstract template that cannot be directly
activated, but can use as a wrapper for our views
● ui-sref-active - directive that adds class when state is active
● much more...
http://angular-ui.github.io/ui-router/site/#/api
Summary
you will probably choose to use ui-router for
your project. basically because it supports
everything the normal ngRoute can do, as well
of as many extra features and functions that is
crucial for large scale applications.
nirkaufman@gmail.com
Migrating to ui-router
nirkaufman@gmail.com
if you are allready use ngRoute, you can start by replacing your routes with simple
states for a good start:
$stateProvider
.state('home',{
url: ‘/home’,
templateUrl: ‘home.html’
})
$routeProvider
.when('/home',{
templateUrl: ‘home.html’
})
<a href=”#/home”>Home</a> <a ui-sref=”home”>Home</a>
$location.url(‘/home’) $state.go(‘home’)
Grab the code:
https://github.com/nirkaufman/angularjs-routing-demo
nirkaufman@gmail.com
il.linkedin.com/in/nirkaufman/
Thank You!
nirkaufman@gmail.com

Mais conteúdo relacionado

Mais procurados

Angular interview questions
Angular interview questionsAngular interview questions
Angular interview questionsGoa App
 
Introduction to angular with a simple but complete project
Introduction to angular with a simple but complete projectIntroduction to angular with a simple but complete project
Introduction to angular with a simple but complete projectJadson Santos
 
Angular 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 formsEyal Vardi
 
stupid-simple-kubernetes-final.pdf
stupid-simple-kubernetes-final.pdfstupid-simple-kubernetes-final.pdf
stupid-simple-kubernetes-final.pdfDaniloQueirozMota
 
Angular Data Binding
Angular Data BindingAngular Data Binding
Angular Data BindingDuy Khanh
 
Angular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP ServicesAngular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP ServicesWebStackAcademy
 
Angular - Chapter 1 - Introduction
 Angular - Chapter 1 - Introduction Angular - Chapter 1 - Introduction
Angular - Chapter 1 - IntroductionWebStackAcademy
 
Angular Interview Questions & Answers
Angular Interview Questions & AnswersAngular Interview Questions & Answers
Angular Interview Questions & AnswersRatnala Charan kumar
 
Angular directives and pipes
Angular directives and pipesAngular directives and pipes
Angular directives and pipesKnoldus Inc.
 
Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming  Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming WebStackAcademy
 
Angular - Chapter 4 - Data and Event Handling
 Angular - Chapter 4 - Data and Event Handling Angular - Chapter 4 - Data and Event Handling
Angular - Chapter 4 - Data and Event HandlingWebStackAcademy
 
Angular Advanced Routing
Angular Advanced RoutingAngular Advanced Routing
Angular Advanced RoutingLaurent Duveau
 

Mais procurados (20)

Angular interview questions
Angular interview questionsAngular interview questions
Angular interview questions
 
Introduction to angular with a simple but complete project
Introduction to angular with a simple but complete projectIntroduction to angular with a simple but complete project
Introduction to angular with a simple but complete project
 
Angular Data Binding
Angular Data BindingAngular Data Binding
Angular Data Binding
 
Angular 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 forms
 
Angular
AngularAngular
Angular
 
Angular Directives
Angular DirectivesAngular Directives
Angular Directives
 
Angular 9
Angular 9 Angular 9
Angular 9
 
Introduction to GraphQL
Introduction to GraphQLIntroduction to GraphQL
Introduction to GraphQL
 
stupid-simple-kubernetes-final.pdf
stupid-simple-kubernetes-final.pdfstupid-simple-kubernetes-final.pdf
stupid-simple-kubernetes-final.pdf
 
Angular Data Binding
Angular Data BindingAngular Data Binding
Angular Data Binding
 
Angular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP ServicesAngular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP Services
 
Angular - Chapter 1 - Introduction
 Angular - Chapter 1 - Introduction Angular - Chapter 1 - Introduction
Angular - Chapter 1 - Introduction
 
Angular Interview Questions & Answers
Angular Interview Questions & AnswersAngular Interview Questions & Answers
Angular Interview Questions & Answers
 
Angular overview
Angular overviewAngular overview
Angular overview
 
Introduction to spring boot
Introduction to spring bootIntroduction to spring boot
Introduction to spring boot
 
Angular directives and pipes
Angular directives and pipesAngular directives and pipes
Angular directives and pipes
 
Angularjs PPT
Angularjs PPTAngularjs PPT
Angularjs PPT
 
Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming  Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming
 
Angular - Chapter 4 - Data and Event Handling
 Angular - Chapter 4 - Data and Event Handling Angular - Chapter 4 - Data and Event Handling
Angular - Chapter 4 - Data and Event Handling
 
Angular Advanced Routing
Angular Advanced RoutingAngular Advanced Routing
Angular Advanced Routing
 

Destaque

ui-router and $state
ui-router and $stateui-router and $state
ui-router and $stategarbles
 
Angular JS Routing
Angular JS RoutingAngular JS Routing
Angular JS Routingkennystoltz
 
Dive into AngularJS Routing
Dive into AngularJS RoutingDive into AngularJS Routing
Dive into AngularJS RoutingEgor Miasnikov
 
Redux with angular 2 - workshop 2016
Redux with angular 2 - workshop 2016Redux with angular 2 - workshop 2016
Redux with angular 2 - workshop 2016Nir Kaufman
 
AngularJS performance & production tips
AngularJS performance & production tipsAngularJS performance & production tips
AngularJS performance & production tipsNir Kaufman
 
SMS based train ticket booking - Customer Education Presentation
SMS based train ticket booking - Customer Education PresentationSMS based train ticket booking - Customer Education Presentation
SMS based train ticket booking - Customer Education PresentationSaarthii
 
How routing works in angular js
How routing works in angular jsHow routing works in angular js
How routing works in angular jscodeandyou forums
 
Webpack and angularjs
Webpack and angularjsWebpack and angularjs
Webpack and angularjsNir Kaufman
 
AngularJS - Services
AngularJS - ServicesAngularJS - Services
AngularJS - ServicesNir Kaufman
 
Data Structures in javaScript 2015
Data Structures in javaScript 2015Data Structures in javaScript 2015
Data Structures in javaScript 2015Nir Kaufman
 
Angular promises and http
Angular promises and httpAngular promises and http
Angular promises and httpAlexe Bogdan
 
redux and angular - up and running
redux and angular - up and runningredux and angular - up and running
redux and angular - up and runningNir Kaufman
 
Up & running with ECMAScript6
Up & running with ECMAScript6Up & running with ECMAScript6
Up & running with ECMAScript6Nir Kaufman
 
Angular js - 10 reasons to choose angularjs
Angular js - 10 reasons to choose angularjs Angular js - 10 reasons to choose angularjs
Angular js - 10 reasons to choose angularjs Nir Kaufman
 
Sms pro - bulk SMS sending software
Sms pro - bulk SMS sending softwareSms pro - bulk SMS sending software
Sms pro - bulk SMS sending softwareLive Tecnologies
 

Destaque (20)

ui-router and $state
ui-router and $stateui-router and $state
ui-router and $state
 
Angular JS Routing
Angular JS RoutingAngular JS Routing
Angular JS Routing
 
UI-Router
UI-RouterUI-Router
UI-Router
 
Dive into AngularJS Routing
Dive into AngularJS RoutingDive into AngularJS Routing
Dive into AngularJS Routing
 
Redux with angular 2 - workshop 2016
Redux with angular 2 - workshop 2016Redux with angular 2 - workshop 2016
Redux with angular 2 - workshop 2016
 
AngularJS performance & production tips
AngularJS performance & production tipsAngularJS performance & production tips
AngularJS performance & production tips
 
SMS based train ticket booking - Customer Education Presentation
SMS based train ticket booking - Customer Education PresentationSMS based train ticket booking - Customer Education Presentation
SMS based train ticket booking - Customer Education Presentation
 
How routing works in angular js
How routing works in angular jsHow routing works in angular js
How routing works in angular js
 
Webpack and angularjs
Webpack and angularjsWebpack and angularjs
Webpack and angularjs
 
AngularJS - Services
AngularJS - ServicesAngularJS - Services
AngularJS - Services
 
Ui router
Ui routerUi router
Ui router
 
Data Structures in javaScript 2015
Data Structures in javaScript 2015Data Structures in javaScript 2015
Data Structures in javaScript 2015
 
Angular promises and http
Angular promises and httpAngular promises and http
Angular promises and http
 
redux and angular - up and running
redux and angular - up and runningredux and angular - up and running
redux and angular - up and running
 
Up & running with ECMAScript6
Up & running with ECMAScript6Up & running with ECMAScript6
Up & running with ECMAScript6
 
Solid angular
Solid angularSolid angular
Solid angular
 
Angular js - 10 reasons to choose angularjs
Angular js - 10 reasons to choose angularjs Angular js - 10 reasons to choose angularjs
Angular js - 10 reasons to choose angularjs
 
Angular redux
Angular reduxAngular redux
Angular redux
 
Sms pro - bulk SMS sending software
Sms pro - bulk SMS sending softwareSms pro - bulk SMS sending software
Sms pro - bulk SMS sending software
 
Web Config
Web ConfigWeb Config
Web Config
 

Semelhante a Choosing Between ngRoute and UI-Router

Workshop 13: AngularJS Parte II
Workshop 13: AngularJS Parte IIWorkshop 13: AngularJS Parte II
Workshop 13: AngularJS Parte IIVisual Engineering
 
Architecture, Auth, and Routing with uiRouter
Architecture, Auth, and Routing with uiRouterArchitecture, Auth, and Routing with uiRouter
Architecture, Auth, and Routing with uiRouterChristopher Caplinger
 
Angularjs - lazy loading techniques
Angularjs - lazy loading techniques Angularjs - lazy loading techniques
Angularjs - lazy loading techniques Nir Kaufman
 
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 serverSpike Brehm
 
CFUGbe talk about Angular JS
CFUGbe talk about Angular JSCFUGbe talk about Angular JS
CFUGbe talk about Angular JSAlwyn Wymeersch
 
ngNewRouter
ngNewRouterngNewRouter
ngNewRouterphidong
 
Front end development with Angular JS
Front end development with Angular JSFront end development with Angular JS
Front end development with Angular JSBipin
 
Patterns Are Good For Managers
Patterns Are Good For ManagersPatterns Are Good For Managers
Patterns Are Good For ManagersAgileThought
 
Building a dashboard using AngularJS
Building a dashboard using AngularJSBuilding a dashboard using AngularJS
Building a dashboard using AngularJSRajthilakMCA
 
AngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get startedAngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get startedStéphane Bégaudeau
 
Adding User Management to Node.js
Adding User Management to Node.jsAdding User Management to Node.js
Adding User Management to Node.jsDev_Events
 
First Steps in Drupal Code Driven Development
First Steps in Drupal Code Driven DevelopmentFirst Steps in Drupal Code Driven Development
First Steps in Drupal Code Driven DevelopmentNuvole
 
Maciej Treder ''Angular Universal - a medicine for the Angular + SEO/CDN issu...
Maciej Treder ''Angular Universal - a medicine for the Angular + SEO/CDN issu...Maciej Treder ''Angular Universal - a medicine for the Angular + SEO/CDN issu...
Maciej Treder ''Angular Universal - a medicine for the Angular + SEO/CDN issu...OdessaJS Conf
 
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
AngularJs Superheroic JavaScript MVW Framework Services by Miracle StudiosAngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
AngularJs Superheroic JavaScript MVW Framework Services by Miracle StudiosLearnimtactics
 
A Story about AngularJS modularization development
A Story about AngularJS modularization developmentA Story about AngularJS modularization development
A Story about AngularJS modularization developmentJohannes Weber
 

Semelhante a Choosing Between ngRoute and UI-Router (20)

Workshop 13: AngularJS Parte II
Workshop 13: AngularJS Parte IIWorkshop 13: AngularJS Parte II
Workshop 13: AngularJS Parte II
 
Architecture, Auth, and Routing with uiRouter
Architecture, Auth, and Routing with uiRouterArchitecture, Auth, and Routing with uiRouter
Architecture, Auth, and Routing with uiRouter
 
Angularjs - lazy loading techniques
Angularjs - lazy loading techniques Angularjs - lazy loading techniques
Angularjs - lazy loading techniques
 
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
 
AngularJs-training
AngularJs-trainingAngularJs-training
AngularJs-training
 
Angular routing
Angular routingAngular routing
Angular routing
 
CFUGbe talk about Angular JS
CFUGbe talk about Angular JSCFUGbe talk about Angular JS
CFUGbe talk about Angular JS
 
ngNewRouter
ngNewRouterngNewRouter
ngNewRouter
 
Angular Workshop_Sarajevo2
Angular Workshop_Sarajevo2Angular Workshop_Sarajevo2
Angular Workshop_Sarajevo2
 
Introduction to angular js
Introduction to angular jsIntroduction to angular js
Introduction to angular js
 
Front end development with Angular JS
Front end development with Angular JSFront end development with Angular JS
Front end development with Angular JS
 
Patterns Are Good For Managers
Patterns Are Good For ManagersPatterns Are Good For Managers
Patterns Are Good For Managers
 
Building a dashboard using AngularJS
Building a dashboard using AngularJSBuilding a dashboard using AngularJS
Building a dashboard using AngularJS
 
AngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get startedAngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get started
 
Adding User Management to Node.js
Adding User Management to Node.jsAdding User Management to Node.js
Adding User Management to Node.js
 
AngularJS
AngularJSAngularJS
AngularJS
 
First Steps in Drupal Code Driven Development
First Steps in Drupal Code Driven DevelopmentFirst Steps in Drupal Code Driven Development
First Steps in Drupal Code Driven Development
 
Maciej Treder ''Angular Universal - a medicine for the Angular + SEO/CDN issu...
Maciej Treder ''Angular Universal - a medicine for the Angular + SEO/CDN issu...Maciej Treder ''Angular Universal - a medicine for the Angular + SEO/CDN issu...
Maciej Treder ''Angular Universal - a medicine for the Angular + SEO/CDN issu...
 
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
AngularJs Superheroic JavaScript MVW Framework Services by Miracle StudiosAngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
 
A Story about AngularJS modularization development
A Story about AngularJS modularization developmentA Story about AngularJS modularization development
A Story about AngularJS modularization development
 

Mais de Nir Kaufman

Angular Dependency Injection
Angular Dependency InjectionAngular Dependency Injection
Angular Dependency InjectionNir Kaufman
 
Angular Prestige: Less-known API and techniques
Angular Prestige: Less-known API and techniquesAngular Prestige: Less-known API and techniques
Angular Prestige: Less-known API and techniquesNir Kaufman
 
Angular CLI custom builders
Angular CLI custom buildersAngular CLI custom builders
Angular CLI custom buildersNir Kaufman
 
Electronic music 101 for developers
Electronic music 101 for developersElectronic music 101 for developers
Electronic music 101 for developersNir Kaufman
 
Nestjs MasterClass Slides
Nestjs MasterClass SlidesNestjs MasterClass Slides
Nestjs MasterClass SlidesNir Kaufman
 
Redux pattens - JSHeroes 2018
Redux pattens - JSHeroes 2018Redux pattens - JSHeroes 2018
Redux pattens - JSHeroes 2018Nir Kaufman
 
Angular EE - Special Workshop by Nir Kaufman
Angular EE - Special Workshop by Nir KaufmanAngular EE - Special Workshop by Nir Kaufman
Angular EE - Special Workshop by Nir KaufmanNir Kaufman
 
Boosting Angular runtime performance
Boosting Angular runtime performanceBoosting Angular runtime performance
Boosting Angular runtime performanceNir Kaufman
 
Decorators in js
Decorators in jsDecorators in js
Decorators in jsNir Kaufman
 
Styling recipes for Angular components
Styling recipes for Angular componentsStyling recipes for Angular components
Styling recipes for Angular componentsNir Kaufman
 
Introduction To Angular's reactive forms
Introduction To Angular's reactive formsIntroduction To Angular's reactive forms
Introduction To Angular's reactive formsNir Kaufman
 
Angular Pipes Workshop
Angular Pipes WorkshopAngular Pipes Workshop
Angular Pipes WorkshopNir Kaufman
 
How Angular2 Can Improve Your AngularJS Apps Today!
How Angular2 Can Improve Your AngularJS Apps Today!How Angular2 Can Improve Your AngularJS Apps Today!
How Angular2 Can Improve Your AngularJS Apps Today!Nir Kaufman
 
Angular2 workshop
Angular2 workshopAngular2 workshop
Angular2 workshopNir Kaufman
 
Angular2 - getting-ready
Angular2 - getting-ready Angular2 - getting-ready
Angular2 - getting-ready Nir Kaufman
 
Angularjs - Unit testing introduction
Angularjs - Unit testing introductionAngularjs - Unit testing introduction
Angularjs - Unit testing introductionNir Kaufman
 

Mais de Nir Kaufman (17)

Angular Dependency Injection
Angular Dependency InjectionAngular Dependency Injection
Angular Dependency Injection
 
Angular Prestige: Less-known API and techniques
Angular Prestige: Less-known API and techniquesAngular Prestige: Less-known API and techniques
Angular Prestige: Less-known API and techniques
 
Angular CLI custom builders
Angular CLI custom buildersAngular CLI custom builders
Angular CLI custom builders
 
Electronic music 101 for developers
Electronic music 101 for developersElectronic music 101 for developers
Electronic music 101 for developers
 
Nestjs MasterClass Slides
Nestjs MasterClass SlidesNestjs MasterClass Slides
Nestjs MasterClass Slides
 
Redux pattens - JSHeroes 2018
Redux pattens - JSHeroes 2018Redux pattens - JSHeroes 2018
Redux pattens - JSHeroes 2018
 
Angular EE - Special Workshop by Nir Kaufman
Angular EE - Special Workshop by Nir KaufmanAngular EE - Special Workshop by Nir Kaufman
Angular EE - Special Workshop by Nir Kaufman
 
Boosting Angular runtime performance
Boosting Angular runtime performanceBoosting Angular runtime performance
Boosting Angular runtime performance
 
Decorators in js
Decorators in jsDecorators in js
Decorators in js
 
Styling recipes for Angular components
Styling recipes for Angular componentsStyling recipes for Angular components
Styling recipes for Angular components
 
Introduction To Angular's reactive forms
Introduction To Angular's reactive formsIntroduction To Angular's reactive forms
Introduction To Angular's reactive forms
 
Webstorm
WebstormWebstorm
Webstorm
 
Angular Pipes Workshop
Angular Pipes WorkshopAngular Pipes Workshop
Angular Pipes Workshop
 
How Angular2 Can Improve Your AngularJS Apps Today!
How Angular2 Can Improve Your AngularJS Apps Today!How Angular2 Can Improve Your AngularJS Apps Today!
How Angular2 Can Improve Your AngularJS Apps Today!
 
Angular2 workshop
Angular2 workshopAngular2 workshop
Angular2 workshop
 
Angular2 - getting-ready
Angular2 - getting-ready Angular2 - getting-ready
Angular2 - getting-ready
 
Angularjs - Unit testing introduction
Angularjs - Unit testing introductionAngularjs - Unit testing introduction
Angularjs - Unit testing introduction
 

Último

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 

Último (20)

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 

Choosing Between ngRoute and UI-Router

  • 2. Routing is mandatory. For an angular app, we can choose between the official ngRoute, or the popular ui-router. In this talk i will introduce you with both of them so you can choose what's fits your needs. nirkaufman@gmail.com
  • 3. Spoiler! In the end of this talk you will probably choose to use ui-router for your project. nirkaufman@gmail.com
  • 4. What's the plan? - Exploring the modules core features and API. - Asking questions & getting answers but most important.. nirkaufman@gmail.com
  • 5. Seeing it in Action! Walking through a working demo project. find the github link in the last slide nirkaufman@gmail.com
  • 6. ngRoute Used to be baked into Angular core, separated into it`s own module in version 1.2. Provides a lot of functionality we expected from a routing system. nirkaufman@gmail.com
  • 7. Installation nirkaufman@gmail.com use bower (or download manually) the angular-route.js file. Make sure to load the module after angular. specify ngRoute as a dependency for your module $ bower install angular-route <script src="angular.js"> </script> <script src="angular-route.js"> </script> angular.module('app', ['ngRoute']);
  • 8. Simple route nirkaufman@gmail.com angular.module('app', ['ngRoute']) .config(function ($routeProvider) { $routeProvider .when('/home',{ templateUrl: ‘views/home.html’ }) .when('/user',{ templateUrl: ‘views/user.html’ }) .otherwise({ template: “<div>NOT FOUND!</div>” }) In the config section of our module, we use the $routeProvider to map url`s to the desired views. the most basic route should include an HTML template to render.
  • 9. Navigate & Display Templates nirkaufman@gmail.com <ng-view onload=”fn()”></ng-view> Our template will be rendered between the ng-view tags. this directive will create a new child scope for the template. we can pass a function to the onload attribute. ngRouter Support only one ngView per Application! We can display the requested from html using <a> tags, or from javaScript using the $location service: function UserController ($location) { $location.path(‘/admin’)}
  • 10. Controllers & Parameters nirkaufman@gmail.com we can assign a controller to the view, and access the route parameters by injecting the $routeParams service to our controller .when('/user/:id,{ templateUrl: ‘views/user.html’, controller: ‘UserController’ }) function UserController ($routeParams) { $routeParams.id // 5234 } http://www.yourApp.com/user/5234
  • 11. Attaching custom data nirkaufman@gmail.com We can extend the route object to include extra data that we might need. .when('/admin,{ templateUrl: ‘views/user.html’, controller: ‘UserController’, permissions: {level : ‘admin’, key: ‘23f’} }) ... function UserController ($route) { permissions = $route.current.permissions }
  • 12. Using resolve nirkaufman@gmail.com We can define a map of factory functions that will be resolved, and injected to our controller.. if any of them is a promise, the route will hold until the resolved. it can be used to load additional resources on demand or fetching data from a remote server. .when('/admin,{ templateUrl: ‘views/user.html’, controller: ‘UserController’, resolve: {data: function() {return “info”} } }) function UserController (data) {...}
  • 13. Route LIfe cycle & events nirkaufman@gmail.com Url Requested $routeChangeStart $routeChangeError $routeChangeSuccess ng-view kicks in$viewContentLoaded onload function
  • 14. ngView in Action nirkaufman@gmail.com $routeChangeSucsses broadcasted create New Scope destroy last scope, remove the last template Link the new Template with the new Scope Link the controller Emit $viewContentLoaded run the onload function
  • 15. Things we didn't cover nirkaufman@gmail.com ● $locationProvider - configure how the application deep linking paths are stored (enable HTML5 mode, and define an hash prefix) ● $location - Represents the URL object as a set of methods (protocol, host, port, path, search, hash) ● $route.reload() - a method that reloads the current view be causing the ng-view directive to create new child scope and re-instantiate the controller ● ngView autoscroll - calling to the $anchorScroll service https://docs.angularjs.org/api/ngRoute/service/$route
  • 16. UI Router UI Router is a routing system for AngularJS that based around the concept of states which may optionally have routes, as well as other behaviours. nirkaufman@gmail.com
  • 17. Define: state. ❏ a ‘place’ in the application in terms of UI and navigation. ❏ a state describes what the UI looks like and does at that place. nirkaufman@gmail.com
  • 18. Installation use bower (or download manually) the angular-ui-router.js file. Make sure to load the module after angular. specify ui.router as a dependency for your module nirkaufman@gmail.com $ bower install angular-ui-router <script src="angular.js"> </script> <script src="angular-ui-router.js"> </script> angular.module('app', ['ui.router']);
  • 19. Simple State nirkaufman@gmail.com angular.module('app', ['ngRoute']) .config(function ($stateProvider) { $stateProvider .state('home',{ url: ‘/home.html’, templateUrl: ‘views/home.html’ }) In the config section of our module, we use the $stateProvider to define a state object and give it a name
  • 20. Navigate & Display Templates nirkaufman@gmail.com <ui-view> </ui-view> Our template will be rendered between the ui-view tags. ngRouter Support multiply ui-views per application! We can display the requested from html using <a ui-sref=’stateName’> tags with the or using the $state service method: function UserController ($state) { $state.go(‘home’)}
  • 21. Controllers & Parameters nirkaufman@gmail.com just like $routeProvider, we can assign a controller to the state, and access the state parameters by injecting the $stateParams service to our controller .state('user,{ url: ‘/user/:id’ templateUrl: ‘views/user.html’, controller: ‘UserController’ }) function UserController ($stateParams) { $stateParams.id // 5234 } http://www.yourApp.com/user/5234
  • 22. Attaching custom data nirkaufman@gmail.com Another powerful feature is the ability to display different views in parallel: .state('home',{ controller: ‘HomeController’ data : { level: ‘admin’ }} ... function HomeController ($state) { data = $state.current.data }
  • 23. Nested Views nirkaufman@gmail.com One of the powerful features of ui router is the ability to define nested views: $stateProvider .state('home',{ url: ‘/home’, templateUrl: ‘views/home.html’ }) .state('home.subsection,{ url: ‘/subsection’, templateUrl: ‘views/section.html’ })
  • 24. Named Views nirkaufman@gmail.com Another powerful feature is the ability to display different views in parallel: $stateProvider .state('home',{ views: { "": { template: "<h1>HELLO!</h1>" }, "sidebar": { template: "<sidebar/>" }, "footer": { template: "<data_thing/>" }}... index.html: <div ui-view></div> <div ui-view="sidebar"></div> <div ui-view="footer"></div>
  • 25. State Callbacks nirkaufman@gmail.com We can optionally define two callbacks to the state object, that will be fired when a state beacom active or inactive, the callbacks can access to the resolved attributes .state('home',{ resolve : { user: function () {...} } onEnter : function (user) {}, onExit : function (user) {} }
  • 26. State LIfe cycle & events nirkaufman@gmail.com state Requested $stateChangeStart $stateChangeError $stateChangeSucsses ui-view kicks in $viewContentLoaded onload function Done! $stateNotFound
  • 27. Things we didn't cover nirkaufman@gmail.com ● $state API - The $state service contain more methods beside go that we take advantage of ● $templateFactory- a utility for defining templates in different ways ● state inheritance - child states inherited the resolves from their parent state ● abstract - we can define an abstract template that cannot be directly activated, but can use as a wrapper for our views ● ui-sref-active - directive that adds class when state is active ● much more... http://angular-ui.github.io/ui-router/site/#/api
  • 28. Summary you will probably choose to use ui-router for your project. basically because it supports everything the normal ngRoute can do, as well of as many extra features and functions that is crucial for large scale applications. nirkaufman@gmail.com
  • 29. Migrating to ui-router nirkaufman@gmail.com if you are allready use ngRoute, you can start by replacing your routes with simple states for a good start: $stateProvider .state('home',{ url: ‘/home’, templateUrl: ‘home.html’ }) $routeProvider .when('/home',{ templateUrl: ‘home.html’ }) <a href=”#/home”>Home</a> <a ui-sref=”home”>Home</a> $location.url(‘/home’) $state.go(‘home’)