58
COMPONENTS ANGULAR + Shawn McKay @Sh_McK [email protected]

Angular + Components

Embed Size (px)

Citation preview

Page 1: Angular + Components

COMPONENTSA N G U L A R +

Shawn McKay@Sh_McK

[email protected]

Page 2: Angular + Components

TODAYCOMPONENT ARCHITECTURE,WEB COMPONENTS,ANGULAR 2 COMPONENTS,THE NEW ANGULAR COMPONENT-ROUTER

Page 3: Angular + Components

CHALLENGE: DRAW A PLAN TO CREATE THIS WEBSITE.

Page 4: Angular + Components

WHAT’S A COMPONENT?

Page 5: Angular + Components

COMPONENT = MVC

Page 6: Angular + Components

COMPONENTS = LEGO

Page 7: Angular + Components

COMPONENTS = TREES

Page 8: Angular + Components

THEN WHAT’S A WEB COMPONENT?

Page 9: Angular + Components

CUSTOM ELEMENTS

HTML IMPORTS

<TEMPLATE>

SHADOW DOM

Page 10: Angular + Components

DEVELOPERS VS. DESIGNERS

Page 11: Angular + Components
Page 12: Angular + Components

“SOMEBODY’S PROBABLY ALREADY MADE AN ANGULAR LIBRARY FOR THAT”

- LAZY/SMART DEVELOPER

Page 13: Angular + Components

CURRENTLY: DEVELOPERS + ANGULAR

Page 14: Angular + Components

“SOMEBODY’S PROBABLY ALREADY MADE A JQUERY LIBRARY FOR THAT”

- LAZY/SMART DESIGNER

Page 15: Angular + Components

CURRENTLY: DESIGNERS + JQUERY

Page 16: Angular + Components

JQUERY EVERYTHING!

Page 17: Angular + Components

WHY NOT JQUERY EVERYTHING?

Page 18: Angular + Components

WEB COMPONENTS

• Re-usable

• Portable

• Consumable

• Consistent

• Maintainable

• Encapsulated

• Quick to use

• Self-describing

• Just drop it on the page and it works!

• Not framework specific

Page 19: Angular + Components

“SOMEBODY’S PROBABLY ALREADY MADE A WEB COMPONENT FOR THAT”- LAZY DEVELOPER/DESIGNER, EVERYONE AND HIS/HER GRANDMA

Page 21: Angular + Components

DRAG & DROP WEBSITES

Page 22: Angular + Components

COMPONENTS EVERYTHING

Page 23: Angular + Components

AN NPM FOR COMPONENTS: DUO.JS

Page 24: Angular + Components

ANGULAR COMPONENTS

Page 25: Angular + Components

COMPONENT = MVC

Page 26: Angular + Components

1.X: DIRECTIVES

.directive('hiGreeter', hiGreeter);

function hiGreeter() { return { template: '<h1>Hello, {{hi.place}}</h1>', controllerAs: 'hi', controller: function () { this.place = "world" } };}

Page 27: Angular + Components

function hiGreeter() { return { priority: 0, terminal: false, template: '<h1>Hello</h1>', controller: 'MyCtrl', controllerAs: 'MyCtrl', bindToController: true, templateNamespace: 'html', transclude: true, replace: false, multiElement: false, restrict: 'A', require: '^parentCtrl', scope: {

item: '@', tool: '=', func: '?&' }, compile: function (el) { return { pre: function(scope, el, attrs) {}, post: function(scope, el, attrs) {} } }};}

1.X: DIRECTIVE DEFINITION OBJECT

Page 28: Angular + Components

EVERYTHING IN ANGULAR 1.X IS A DIRECTIVE

Page 29: Angular + Components

ANGULAR 2: 3 KINDS OF DIRECTIVES

COMPONENT, DECORATOR, VIEWPORT

Page 30: Angular + Components

2.X: COMPONENT

Page 31: Angular + Components

ES6/TYPESCRIPT

@Component({selector: 'hi-greeter'})@View({ template: '<h1>Hello, {{hi.place}}</h1>'})class Hi { constructor() { this.place = 'world'; }}

Page 32: Angular + Components

ES5 (A)

a.Component({selector: 'hi-greeter'}).View({ template: '<h1>Hello, {{hi.place}}</h1>'}).for(Hi)function Hi() { return { this.place = 'world'; };}

Page 33: Angular + Components

ANGULAR 2 = TREES

Page 34: Angular + Components

AN AMAZING HISTORY OF ROUTERS

I N T O

T H E

F U T U R E

Page 35: Angular + Components

NG-ROUTER

• Map pages to urls

• Pass in params

$routeProvider.when('/', {

templateUrl: 'pages/home.html', controller: 'mainController' }).when('/about', {

templateUrl: 'pages/about.html', controller: 'aboutController' });

}

Page 36: Angular + Components

UI-ROUTER

Ng-router features +

Nested states/views

Named states

Resolve data

Route data

Abstract states (inheritance)

onChange, onChangeError

$stateProvider .state('contacts', { abstract: true, url: '/contacts', template: '<ui-view/>'

}) .state('contacts.list', {

url: '/list', views: { 'list': {

templateUrl: 'contacts-list.html’, controller: 'ContactListController', resolve:{ list: function(contacts){

return {'list': contacts.all()};}}},

'info': { templateUrl: 'contacts-view.html',

controller: 'ContactViewController' }

});

Page 37: Angular + Components

<div router-viewport="name"></div>

COMPONENT-ROUTER

Ui-router features +

Routes to components

Naming conventions

Lifecycle hooks

Lazy-loading

Change page titles

$routeConfig = [ { path: '/', component: 'home' }, { path: '/contacts/’,

components: { 'list': 'contactList', 'view': 'contactView' } }];

Page 38: Angular + Components

ROUTER PARTS

Page 39: Angular + Components

WARNING: UNDER DEVELOPMENTEXPECTED RELEASE: maybe next week

?

Things change, but the concepts stay the same.

Page 40: Angular + Components

$ROUTECONFIG

// Routes bind to a controller/class

MyController.$routeConfig = [ {

path: '/user',

component: 'user’, // if named paths, use components: { name: ‘value’, etc. }

as: ‘userState’

} ];

Page 41: Angular + Components

SETUP

<a ng-link=“home”></a> // link to component name

<div ng-viewport></div>

// named routes

<a ng-link=“master:home”></a>

<div ng-viewport=“master”></div>

// params

<a ng-link=“home({greeting: ‘hello’})></a> // accessed by $routeParams

Page 42: Angular + Components

NAMING CONVENTIONS

Page 43: Angular + Components

$stateProvider .state('contacts', { abstract: true, url: '/contacts', template: '<ui-view/>'

}) .state('contacts.list', {

url: '/list', views: { 'list': {

templateUrl: 'contacts-list.html’, controller: 'ContactListController', resolve:{ list: function(contacts){

return {'list': contacts.all()};}}},

'info': { templateUrl: 'contacts-view.html',

controller: 'ContactViewController' }

});

Page 44: Angular + Components

COMPONENT ROUTER

$routeConfig = [ { path: '/', component: 'home' }, { path: '/contacts/’,

components: { 'list': 'contactList', 'view': 'contactView' } }];

Page 45: Angular + Components

COMPONENT: HOME

{ path: '/', component: 'home' templateUrl: './components/home/home.html’, controller: 'HomeController', controllerAs: 'home'}

Page 46: Angular + Components

COMPONENT: CONTACTLIST

{ path: '/', component: 'contactList', templateUrl: './components/contact-list/contact-list.html', controller: 'ContactListController', controllerAs: 'contactList’}

Page 48: Angular + Components

LIFECYCLE HOOKS

Page 49: Angular + Components
Page 50: Angular + Components

1. CONTROLLER INSTANTIATED

THINK

Lazy-load

Dependencies added

angular.module('app', []) .controller('MyController', ['user', '$http', MyController]);

function MyController(user, $http) { this.user = user; this.$http = $http;}

Page 51: Angular + Components

2. CANACTIVATE

THINK

Security

Permission

Before route can change

function MyController(user) { this.user = user;}

MyController.prototype.canActivate = function() { return this.user.isAdmin;};

Page 52: Angular + Components

3. ACTIVATE

THINK

Data

Resolve

Processing

Before route change finishes

function MyController($http) { this.$http = $http;}

MyController.prototype.activate = function() { return this.files = this.$http.downloadFiles();};

Page 53: Angular + Components

4. CANDEACTIVATEBefore each old-route component is removedCalled for each component

THINK

Save Data

Save State

function SaveController($q) { this.$q = $q;}SaveController.prototype.canDeactivate = function() { this.deferred = this.$q.defer(); this.showSaveDialog = true; // return true = can continue return this.deferred.promise();};

Page 54: Angular + Components

5. DEACTIVATEFired for each component removedBut called after canActivate of next route

THINK

Clean upSomeController.prototype.deactivate = function() { return this.removeSelf();};

Page 55: Angular + Components
Page 57: Angular + Components

QUIZ

• WHAT’S A COMPONENT?

• WHAT’S A WEB COMPONENT?

• WHY SHOULD I CARE ABOUT COMPONENTS?

• WHAT ARE THE BENEFITS OF THE NEW COMPONENT ROUTER?

Page 58: Angular + Components

COMPONENTSA N G U L A R +

Shawn McKay@Sh_McK

[email protected]