1 of 25

Angular2

触ってみた

tyfkda

2016/03/30 ng-sake

2 of 25

自己紹介

  • tyfkda/福田強
  • AngularJS歴:半年
    • 1.4.7→1.5
  • ES6
  • 業務やマイグレーションではないけど、Angular2を触ってみました

3 of 25

AngularJS1のあの機能を

Angular2で

4 of 25

基本的な項目

  • コンポーネント
  • ルーター
  • バインディング
  • *ngFor, *ngIf
  • [class.active]=”active”
    • ngClass
  • [style.background-color]=”’red’”
    • ngStyle
  • [hidden]=”true”
    • [style.dipslay]=”’none’” とは別

5 of 25

ng-show

  • 一般的には*ngIfを使うのがよい
  • [style.display]=”cond ? ’’ : ’none’”
  • [hidden]

6 of 25

myShow (impl)

import {Directive, ElementRef, Input} from ’angular2/core’�@Directive({selector: ’[myShow]’})�export class ShowDirective {� constructor(private elRef: ElementRef) {}� @Input() set myShow(cond: boolean) {� this.elRef.nativeElement.style.display =� cond ? ’’ : ’none’� }�}

Attribute Directive

7 of 25

$scope.$on(‘$destroy’, ...)

  • ngOnDestroy()

Lifecycle Hooks

8 of 25

ng-transclude

  • <ng-content></ng-content>
  • 選択も可能 <ng-content select=”foo”>

“Transclusion” in Angular 2

9 of 25

親子コンポーネントの連携

例:タブコンポーネント

<tabs>� <tab tabTitle=”Tab 1”>blah blah</tab>�</tabs>

  • 親:Tabs
    • @ContentChildren(Tab) tabs: QueryList<Tab>
  • 子:Tab
    • 親のことを知る必要がない
    • 親(外)に通知するには@Output

Creating a tabs component

10 of 25

$scope.$digest()

  • 値の更新を通知

constructor(private cdRef: ChangeDetectorRef) {}�� onSomethingHappened() {� this.cdRef.detectChanges()� }�

11 of 25

ng-template + ng-include

  • ない… src で切り替えられて便利だったんだけど…
  • やるとしたら、自前でコンポーネント差し替え?

12 of 25

$timeout

  • DOMに反映されてからなんかする、みたいな用途で使ってた
  • Lifecycle Hooksどおりやれば、使わなくて済みそう?

13 of 25

ここがすごいよ

Angular2

14 of 25

CSSがコンポーネント単位で定義できる

  • CSSの管理が楽になる

15 of 25

Observable, async pipe

<input type=”text” [ngFormControl]=”term”>�term = new Control()�this.term.valueChanges...

items: Observable<Array<string>>�<li *ngFor=”#item of items | async”>

Taking advantage of Observables in Angular 2, Part 2

16 of 25

EventEmitter

import {EventEmitter, Output} from ‘angular2/core’

@Output() myEvent = new EventEmitter()

this.myEvent.emit(123)

<myComp [myEvent]=”foo($event)”></myComp>

17 of 25

デモ

18 of 25

Angular2 + Firebase

19 of 25

初期化

this.movesRef = new Firebase(url)

20 of 25

セルがクリックされたら

cellClicked(cell:{x:number, y:number}) {� const move = {x:cell.x, y:cell.y, stone:this.turn}� this.movesRef.push(move)�}

21 of 25

Firebase -> Observable

class FirebaseService {� on(ref:Firebase, eventType:string) {� return Rx.Observable.create(observer => {� const callback = snapshot => {� observer.next(snapshot)� }� ref.on(eventType, calllback)� return () => { ref.off(eventType, handler) }� })� }�}

22 of 25

Subscribe

this.subscription =� this.firebaseService.on(this.movesRef, ‘child_added’)� .map(snapshot => snapshot.val())� .subscribe(move => {� this.board.putStone(move.x, move.y, move.stone)� })

23 of 25

Unsubscribe

this.subscription.unsubscribe()

24 of 25

感想

25 of 25

触ってみた感想

  • 複雑だったdirective周りがcomponentで簡潔になった
  • コンポーネント単位でまとめられて、さらにコンポーネント間の連携も簡単にできるので、うまいこと部品化できそう
  • RxJSの導入によって、親子の連携がしやすい
  • 「書式がキモい」はすぐ慣れる、むしろ自然に感じてくる
  • 環境構築や実行時エラーがまだ慣れない…