@Computed
@StateRepository()
@State<OrderLineModel>({
name: 'orderLine',
defaults: {
price: 0,
amount: 1,
},
})
@Injectable()
class OrderLineState extends NgxsDataRepository<OrderLineModel> {
@Computed()
public get total(): number {
return this.snapshot.price * this.snapshot.amount;
}
}Using values from other states
@StateRepository()
@State({
name: 'price',
defaults: 10,
})
@Injectable()
class PriceState extends NgxsDataRepository<number> {
public setPrice(value: string): void {
this.setState(parseFloat(value));
}
}
@StateRepository()
@State({
name: 'amount',
defaults: 20,
})
@Injectable()
class AmountState extends NgxsDataRepository<number> {
constructor(private readonly price: PriceState) {
super();
}
@Computed()
public get sum(): number {
return this.snapshot + this.price.snapshot;
}
public setAmount(value: string): void {
this.setState(parseFloat(value));
}
}
@Component({
selector: 'app',
template: `
<input
[ngModel]="price.snapshot"
(ngModelChange)="price.setPrice($event)"
placeholder="Price"
/>
<br />
<input
[ngModel]="amount.snapshot"
(ngModelChange)="amount.setAmount($event)"
placeholder="Amount"
/>
<p>Sum: {{ amount.sum }}</p>
`,
})
class AppComponent {
constructor(
public price: PriceState,
public amount: AmountState,
) {}
}
Now Selector and Select utilities are not needed, you can do it right in the state
Selector and Select utilities are not needed, you can do it right in the stateMeta selectors
forkJoin vs combineLatest
Side effects (non observables)
Last updated