Model pattern for Angular by @tomastrajan 
-
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.
- Check out Demo & Documentation
- Check out Blog Post
- Create
model.service.ts
with following content. - Import and provide
MODEL_PROVIDER
(frommodel.service.ts
) constant in yourCoreModule
(useAppModule
in case you don't haveCoreModule
) - Use model in your own services. Import
ModelFactory
and inject it in service's constructor, then create model instance withthis.model = this.modeFactory.create(initialData)
. Expose model with descriptively named variable (eg:this.todos$ = this.model.data$
) - Use service in your component. Import and inject service into components constructor.
Subscribe to services data in template
todosService.todos$ | async
or explicitlythis.todosService.todos$.subscribe(todos => { /* ... */ })
- todos.service.ts - creating model instance and mutating model data
- todos.component.ts - consuming model
- routes - for initializing of model data before navigation
Built with Angular CLI