New
Router
Component
Chris Sevilleja
scotch.io
Angular Articles by Chris and scotch.io
https://scotch.io/tag/angular-js
This Presentation GitHub Repo
https://github.com/scotch-io/angular-router-1x
Presentation Video
https://www.youtube.com/watch?v=DGT3Htcqygk
Component Router GitHub Repo
Simple
No nested views
No sibling views
Good for current applications
Not built for migration
Greatly inspired component router
@Component({
selector: 'articles'
})
@View({
inline: "<div>Look! I'm at {{ confName }}!</div>"
})
class ArticlesComponent {}
$stateProvider
.state({
url : '/articles',
templateUrl : 'components/articles/articles.html',
controller : 'ArticlesController',
controllerAs: 'articles'
});
ui.router
$stateProvider.state()
<ui-view>
ui-sref
ngComponentRouter
$router.config()
<ng-outlet>
ng-link
|-- components/
|-- articles/
|-- articles.js
|-- articles.html
|-- app.js
$stateProvider
.state({
url : '/articles',
templateUrl : 'components/articles/articles.html',
controller : 'ArticlesController',
controllerAs: 'articles'
});
$router.config([
{ path: '/articles', component: 'articles' }
]);
./components/<component_name>/<component_name>.html
Template
Controller
Controller As
<Component_name>Controller
<component_name>
./components/articles/articles.html
Template
Controller
Controller As
ArticlesController
articles
|-- controllers/
|-- articles.js
|-- views/
|-- articles.html
|-- app.js
Custom Controller Name
$componentMapper
.setCtrlNameMapping(function(name) {
return name + 'Ctrl';
});
Custom Template Location
$componentMapper
.setTemplateMapping(function(name) {
return './views/' + name + '.html';
});
Module
ArticlesCtrl.$templateUrl = 'views/articles.html';
function ArticlesCtrl {}
Component Mapper Config
$componentMapper
.setTemplateMapping(function(comp) {
var ctrl = $componentMapper.controller(comp);
var template = $controllerIntrospector(name, '$templateUrl');
return template;
});
Move files around for components/
@Component({
selector: 'articles'
})
@View({
inline: "<div>Look! I'm at {{ confName }}!</div>"
})
class ArticlesComponent {}
@View({ templateUrl: 'views/articles.html' })
class ArticlesComp {}
Angular 1 with $controllerIntrospector
Angular 2 Component
ArticlesCtrl.$templateUrl = 'views/articles.html';
function ArticlesCtrl {}
Add dependencies
Set routing configuration
Change directives
angular
.module(...)
.component('articles', {
selector: 'articles',
controller: 'ArticlesController',
templateUrl: 'views/articles.html'
});
Chris Sevilleja
@sevilayha
scotch.io