angular-model-pattern-example icon indicating copy to clipboard operation
angular-model-pattern-example copied to clipboard

Model pattern for Angular (2, 4, ...), manage and share your state with simple services using RxJS Subjects and Observables

Model pattern for Angular by @tomastrajan Build Status

Model pattern is now available as library too !

  • Check out ngx-model or just install library using npm i -S ngx-model.

    All existing documentation is still valid, ngx-model just provides convenient way of using model pattern without need to copy model code by hand.

Documentation

Getting started

  1. Create model.service.ts with following content.
  2. Import and provide MODEL_PROVIDER (from model.service.ts) constant in your CoreModule (use AppModule in case you don't have CoreModule)
  3. Use model in your own services. Import ModelFactory and inject it in service's constructor, then create model instance with this.model = this.modeFactory.create(initialData). Expose model with descriptively named variable (eg: this.todos$ = this.model.data$)
  4. Use service in your component. Import and inject service into components constructor. Subscribe to services data in template todosService.todos$ | async or explicitly this.todosService.todos$.subscribe(todos => { /* ... */ })

Examples

Built with Angular CLI