@@ -103,6 +103,9 @@ npm i -S @angular/material @angular/cdk
103
103
スタイルテーマを追加
104
104
``` styles.scss
105
105
@import " ~@angular/material/prebuilt-themes/indigo-pink.css" ;
106
+ md-list-item {
107
+ overflow : hidden ;
108
+ }
106
109
```
107
110
108
111
app.moduleに使いたいモジュールをimportしておくよ!
@@ -157,7 +160,7 @@ export class AppModule { }
157
160
158
161
ToDoを保持するServiceを作りますよ!
159
162
```
160
- ng g service shared/services/todo
163
+ ng g service shared/services/todo/todo
161
164
```
162
165
163
166
> WARNING Service is generated but not provided, it must be provided to be used
@@ -166,22 +169,22 @@ ServiceはModuleに勝手に読み込まれないので注意です。
166
169
167
170
Serviceで使う型定義書くファイルも一緒に作りましょう。
168
171
```
169
- ng g interface shared/models /todo
172
+ ng g interface shared/services /todo/todo model
170
173
```
171
174
172
- ``` shared/models /todo.ts
175
+ ``` shared/services /todo/todo.model .ts
173
176
export interface TodoItem {
174
- id: number ,
175
- state: string ,
176
- value: string
177
+ id: number ;
178
+ state: string ;
179
+ value: string ;
177
180
}
178
181
```
179
182
180
183
作った型定義を読み込むよ!
181
184
182
185
``` shared/services/todo.service.ts
183
186
import { Injectable } from ' @angular/core' ;
184
- import { TodoItem } from ' ../models/ todo' ; // <-追加
187
+ import { TodoItem } from ' ./ todo.model ' ; // <-追加
185
188
186
189
@Injectable ()
187
190
export class TodoService {
@@ -195,7 +198,7 @@ export class TodoService {
195
198
import { Injectable } from ' @angular/core' ;
196
199
import { Observable } from ' rxjs/Observable' ;
197
200
import { BehaviorSubject } from ' rxjs/BehaviorSubject' ;
198
- import { TodoItem } from ' ../models/ todo' ;
201
+ import { TodoItem } from ' ./ todo.model ' ;
199
202
200
203
@Injectable ()
201
204
export class TodoService {
@@ -313,7 +316,7 @@ ng g pipe shared/pipes/todoSearch
313
316
314
317
``` shared/pipes/todo-search.ts
315
318
import { Pipe , PipeTransform } from ' @angular/core' ;
316
- import { TodoItem } from ' ../models /todo' ;
319
+ import { TodoItem } from ' ../services /todo/todo.model ' ;
317
320
318
321
@Pipe ({
319
322
name: ' todoSearch'
@@ -332,32 +335,20 @@ export class TodoSearchPipe implements PipeTransform {
332
335
``` home/home.component.ts
333
336
import { Component , OnInit } from ' @angular/core' ;
334
337
import { Observable } from ' rxjs/Observable' ;
335
- import { TodoService } from ' ../shared/services/todo.service' ;
336
- import { TodoItem } from ' ../shared/models /todo' ;
338
+ import { TodoService } from ' ../shared/services/todo/todo .service' ;
339
+ import { TodoItem } from ' ../shared/services /todo/todo.model ' ;
337
340
338
341
import { MdRadioChange } from ' @angular/material' ;
339
342
340
- import { slideFadeIn , slideFadeOut } from ' ../app.animations' ;
341
- import {
342
- transition ,
343
- trigger ,
344
- state ,
345
- style ,
346
- stagger ,
347
- query ,
348
- group ,
349
- animateChild
350
- } from ' @angular/animations' ;
351
-
352
343
@Component ({
353
- selector: ' et -home' ,
344
+ selector: ' app -home' ,
354
345
templateUrl: ' ./home.component.html' ,
355
346
styleUrls: [' ./home.component.scss' ]
356
347
})
357
348
export class HomeComponent implements OnInit {
358
349
public list: Observable <TodoItem []>;
359
- public searchWord: string = ' ' ;
360
- public filterState: string = ' all' ;
350
+ public searchWord = ' ' ;
351
+ public filterState = ' all' ;
361
352
public filterItems = [
362
353
{
363
354
name: ' all' ,
@@ -429,6 +420,8 @@ export class HomeComponent implements OnInit {
429
420
</md-radio-group>
430
421
</form>
431
422
423
+ <button md-raised-button color="warn" (click)="clearComplete()">完了したタスクを削除</button>
424
+
432
425
<md-list *ngIf="(list | async | todoSearch : searchWord) as items">
433
426
<md-list-item *ngFor="let item of items" (click)="changeState(item.id)">
434
427
<md-checkbox md-list-icon [checked]="item.state === 'complete'" (click)="$event.preventDefault()"></md-checkbox>
@@ -439,13 +432,8 @@ export class HomeComponent implements OnInit {
439
432
</p>
440
433
</md-list-item>
441
434
</md-list>
442
-
443
- <button md-raised-button color="warn" (click)="clearComplete()">完了したタスクを削除</button>
444
435
```
445
436
446
- ここまではこちらのリポジトリにあります。
447
- https://github.com/frontainer/practice-angular-animations
448
-
449
437
---
450
438
451
439
## アニメーション基本
@@ -471,15 +459,15 @@ import {
471
459
state (' complete' , style ({
472
460
backgroundColor: ' #eee'
473
461
})),
474
- transition (' todo => complete' , [
462
+ transition (' * => complete' , [
475
463
style ({
476
464
backgroundColor: ' #fff'
477
465
}),
478
466
animate (' 200ms ease-out' , style ({
479
467
backgroundColor: ' #eee'
480
468
}))
481
469
]),
482
- transition (' complete => todo' , [
470
+ transition (' * => todo' , [
483
471
style ({
484
472
backgroundColor: ' #eee'
485
473
}),
@@ -493,8 +481,8 @@ import {
493
481
})
494
482
```
495
483
496
- ``` home/home.component.html
497
- <md-list-item *ngFor="let item of items" (click)="changeState(item.id)" [@stateEffect]="item.state">
484
+ ``` home/home.component.html 21行目
485
+ <md-list-item *ngFor="let item of items" (click)="changeState(item.id)" [@stateEffect]="item.state">
498
486
```
499
487
500
488
` [@stateEffect]="item.state" ` を追加しました。
@@ -522,7 +510,7 @@ import {
522
510
4.2で追加されたanimation関数を使うことで共通に使うアニメーションの定義をすることができます。パラメータで実行時間などは変更できるので、ある程度汎用的に作れる様になりますよ!
523
511
524
512
```
525
- ng g class app.animations.ts
513
+ ng g class app.animations
526
514
```
527
515
528
516
代表的なフェードイン・フェードアウトを書いてみましょう。
@@ -552,12 +540,10 @@ export const slideFadeIn = animation([
552
540
export const slideFadeOut = animation ([
553
541
style ({
554
542
opacity: 1 ,
555
- transform: ' translateX(0)' ,
556
543
height: ' *'
557
544
}),
558
545
animate (' {{time}} {{easing}}' , style ({
559
546
opacity: 0 ,
560
- transform: ' translateX(-2%)' ,
561
547
height: 0
562
548
}))
563
549
], {
@@ -591,9 +577,9 @@ componentのanimationsにアニメーションを定義を追加しますよ!
591
577
selector: ' et-home' ,
592
578
templateUrl: ' ./home.component.html' ,
593
579
styleUrls: [' ./home.component.scss' ], // <- 「,」忘れずに
594
- // 追加ここから
595
580
animations: [
596
581
// ...略
582
+ // 追加ここから
597
583
trigger (' slideFade' , [
598
584
transition (' :enter' , [
599
585
useAnimation (slideFadeIn )
@@ -602,12 +588,12 @@ componentのanimationsにアニメーションを定義を追加しますよ!
602
588
useAnimation (slideFadeOut )
603
589
])
604
590
])
591
+ // 追加ここまで
605
592
]
606
- // 追加ここまで
607
593
})
608
594
```
609
595
610
- ``` home/home.component.html
596
+ ``` home/home.component.html 21行目
611
597
<md-list-item *ngFor="let item of items" (click)="changeState(item.id)" [@stateEffect]="item.state" @slideFade>
612
598
```
613
599
@@ -644,6 +630,7 @@ import {
644
630
stagger // <- 追加
645
631
} from '@angular/animations';
646
632
// ...略
633
+ // slideFade置き換えここから
647
634
trigger('slideFade', [
648
635
transition('* => *', [
649
636
query(':leave', [
@@ -656,9 +643,10 @@ trigger('slideFade', [
656
643
], { optional: true })
657
644
])
658
645
])
646
+ // slideFade置き換えここまで
659
647
```
660
648
661
- ``` home/home.component.html
649
+ ``` home/home.component.html 20行目
662
650
<md-list *ngIf="(list | async | todoSearch : searchWord) as items" [@slideFade]="items">
663
651
<md-list-item *ngFor="let item of items" (click)="changeState(item.id)" [@stateEffect]="item.state">
664
652
```
@@ -690,9 +678,10 @@ import {
690
678
} from ' @angular/animations' ;
691
679
692
680
// ...略
681
+ // slideFade置き換えここから
693
682
trigger (' slideFade' , [
694
683
transition (' * => *' , [
695
- group ([ // <- 追加
684
+ group ([ // <- 追加
696
685
query (' :leave' , [
697
686
useAnimation (slideFadeOut )
698
687
], { optional: true }),
@@ -701,9 +690,10 @@ trigger('slideFade', [
701
690
useAnimation (slideFadeIn )
702
691
])
703
692
], { optional: true })
704
- ]) // <- 追加
693
+ ]) // <- 追加
705
694
])
706
695
])
696
+ // slideFade置き換えここまで
707
697
```
708
698
709
699
これで消えつつ表示されるようになりました。
0 commit comments