Skip to content

Instantly share code, notes, and snippets.

View splincode's full-sized avatar
💬
Typing...

Maksim Ivanov splincode

💬
Typing...
View GitHub Profile
@splincode
splincode / code.md
Last active September 16, 2018 10:21

Angular

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  template: `
    <h1>{{ msg }}</h1>
    <h3>{{ reverseMsg() }}</h3>
@State<string[]>({
name: 'animals',
defaults: []
})
export class AnimalsState {
@Receiver()
public static addAnimal(ctx: StateContext<string[]>, action: EmitterAction<string>) {
ctx.setState((state) => [ ...state, action.payload ]);
}
}
@splincode
splincode / counter.ts
Created January 2, 2019 14:09
counter.ts
@State<number>({
name: 'counter',
defaults: 0
})
export class CounterState {
@Action(Increment)
public increment({ setState }: StateContext<number>) {
setState((state) => state++);
}
}
@Component({
selector: 'app-root',
template: `
<ng-container *ngIf="counter$ | async as counter">
<h1>{{ counter }}</h1>
</ng-container>
<button (click)="increment()">Increment</button>
`
})
@Component({
selector: 'app-animal',
template: `
<ng-container *ngFor="let animals of animals$ | async">
<p>{{ animal }}</p>
</ng-container>
<input #inputAnimal placeholder='Add new animal' />
<button (click)="addAnimal.emit(inputAnimal.value)">
import { NgxsModule } from '@ngxs/store';
import { NgxsDispatchPluginModule } from '@ngxs-labs/dispatch-decorator';
...
@NgModule({
imports: [
NgxsModule.forRoot([ ... ]),
NgxsDispatchPluginModule.forRoot()
],
...
export class Increment {
static readonly type = '[Increment]: description';
}
@State<number>({
name: 'count',
defaults: 0
})
export class CountState {
@Action(Increment)
import { Dispatch } from '@ngxs-labs/dispatch-decorator';
import { CounterState, Increment } from './counter.state';
...
@Component({
selector: 'my-app',
template: `
<h1>Count is {{ counter$ | async }}<h1>
<button (click)="increment()">increment</button>
`
import { Dispatch } from '@ngxs-labs/dispatch-decorator';
...
@Component({
selector: 'my-app',
template: `
{{ stateA$ | async }}
{{ stateB$ | async }}
{{ stateC$ | async }}
...
import { Component } from '@angular/core';
import { HeroesService } from './store/heroes/heroes.service';
@Component({
selector: 'my-app',
templateUrl: './app.component.html'
})
export class AppComponent {
constructor(public heroes: HeroesService) { }
}