libs/fruit-basket/src/basket-ui/basket-ui.component.ts
selector | basket-ui |
templateUrl | ./basket-ui.component.html |
Properties |
Methods |
constructor(store: Store
|
||||||
Parameters :
|
empty |
empty()
|
Returns :
void
|
pickApple | ||||||
pickApple(count: number)
|
||||||
Parameters :
Returns :
void
|
pickBerry |
pickBerry()
|
Returns :
void
|
apple |
apple:
|
Type : Observable<number>
|
berry |
berry:
|
Type : Observable<number>
|
Public store |
store:
|
Type : Store<AppState>
|
total |
total:
|
Type : Observable<number>
|
import { Component } from '@angular/core';
import { Store } from '@ngrx/store';
import { Observable } from 'rxjs/Observable';
import { PickBerryAction, AppState, PickApplesAction, EmptyCartAction } from '../state/state';
@Component({
selector: 'basket-ui',
templateUrl: './basket-ui.component.html'
})
export class BasketUiComponent {
berry: Observable<number>;
apple: Observable<number>;
total: Observable<number>;
constructor(public store: Store<AppState>) {
this.berry = store.select(state => state.fruit.berryCounter);
this.apple = store.select(state => state.fruit.appleCounter);
this.total = store.select(state => state.fruit.berryCounter + state.fruit.appleCounter);
}
pickBerry() {
this.store.dispatch(new PickBerryAction());
}
pickApple(count: number) {
this.store.dispatch(new PickApplesAction(count));
}
empty() {
this.store.dispatch(new EmptyCartAction());
}
}
<h3>Fruit Basket</h3>
<button (click)="empty()" class="btn">Empty Cart</button>
<p>Total Fruit: {{ total | async }}</p>
<div class="row">
<counter-display
class="col s12 m6"
label="Berries"
[counter]="berry | async"
(pick)="pickBerry()">
</counter-display>
<counter-display
class="col s12 m6"
label="Apples"
[counter]="apple | async"
(pick)="pickApple($event)">
</counter-display>
</div>