Need help with model?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

263 Stars 22 Forks MIT License 86 Commits 3 Opened issues


Angular Model - Simple state management with minimalist API, one way data flow, multiple model support and immutable data exposed as RxJS Observable.

Services available


Need anything else?

Contributors list

Angular Model - @angular-extensions/model

by @tomastrajan

npm npm npm Build Status codecov Conventional Commits Twitter Follow

Simple state management with minimalistic API, one way data flow, multiple model support and immutable data exposed as RxJS Observable.


@angular-extensions/model dataflow diagram

Getting started in Angular CLI projects

  1. Install

    ng add @angular-extensions/model
  2. Generate model service

    ng g @angular-extensions/model:model examples/todo --items
  3. Use model service in your component. Let's generate new

    ng g component examples/todo --inline-template

    And then adjust component implementation as in the example below

    import { Component } from '@angular/core';
    import { TodoService } from './todo.service';

    @Component({ selector: 'app-todo', template: <!-- template subscription to todos using async pipe --> <ng-container async as todos> <h1>Todos ({{ todos.length }})</h1> <ul> <li todo of todos> {{ todo.prop }} </li> </ul> <button>Add todo</button> </ng-container> , styleUrls: ['./todo.component.css'] }) export class TodoComponent { constructor(public todoService: TodoService) {}

    addTodo() { this.todoService.addTodo({ prop: 'New todo!' }); } }

  4. Use our new

     component in the template of the 

Please mind that you might be using different application prefix than

so adjust accordingly.

Model API

The model has a small API that as shown in in the illustration above.

  • get(): T
    - returns current model value
  • set(data: T): void
    - sets new model value
  • data$: Observable
    - observable of the model data, every call
    will push new model state to to this observable (the data is immutable by default but this can be changed using one of the other provided factory functions as described below)

Check out generated

to see an example of how the model should be used. In general, the service will implement methods in which it will retrieve current model state, mutate it and set new state back to the model. Model will then take care of pushing immutable copies of the new state to all components which are subscribed using

Available Model Factories

Models are created using model factory as shown in example

, check line
this.model = this.modelFactory.create(initialData);
. Multiple model factories are provided out of the box to support different use cases:
  • create(initialData: T): Model
    - create basic model which is immutable by default (
  • createMutable(initialData: T): Model
    - create model with no immutability guarantees (you have to make sure that model consumers don't mutate and corrupt model state) but much more performance because whole cloning step is skipped
  • createMutableWithSharedSubscription(initialData: T): Model
    - gain even more performance by skipping both immutability and sharing subscription between all consumers (eg situation in which many components are subscribed to single model)
  • createWithCustomClone(initialData: T, clone: (data: T) => T)
    - create immutable model by passing your custom clone function (
    cloning doesn't support properties containing function or regex so custom cloning functionality might be needed)
  • createWithConfig(config)
    - create model by passing in config object with values of all configuration properties (
    initialData: T
    immutable: boolean
    sharedSubscription: boolean
    clone: (data: T) => T

Model Schematics API

Model services are generated using Angular CLI. It is a 3rd party schematics so we have to specify it when running

ng g
command like this
ng g @angular-extensions/model: 
. The schematics currently contains only one schematic called

Basic usage

ng g @angular-extensions/model:model example/todo

Supported options

  • --items
    - creates service for collection of items (it will expose
    todos$: Observable;
    instead of
    todo$: Observable
  • --flat
    - generates service file directly in the
    folder without creating folder with the name
  • --spec
    - generate service test file (default:
  • --module
    - will decide how to register service into Angular dependency injection context (service will use
    providedIn: 'root'
    when no module was provided, module can be provided as a path to module relative to the location of generated service, eg
    ng g @angular-extensions/model:model examples/auth --module ../app.module.ts
  • --project
    - project in which to generate the service (for multi project Angular CLI workspaces, will generate service in the first project by default, when no project was provided)


Getting started without Angular CLI

It is also possible to use

in Angular project which do not use Angular CLI.
  1. Install

    npm i -S @angular-extensions/model
  2. Create new model service in

    import { Injectable } from '@angular/core';
    import { Model, ModelFactory } from '@angular-extensions/model';
    import { Observable } from 'rxjs';

    const initialData: Todo[] = [];

    @Injectable({ providedIn: 'root' }) export class TodoService { private model: Model;

    todos$: Observable;

    constructor(private modelFactory: ModelFactory) { this.model = this.modelFactory.create(initialData); this.todos$ =$; }

    addTodo(todo: Todo) { const todos = this.model.get();


    } }

    export interface Todo { prop: string; }

  3. Use new model service in some of your components as described in point 3 and above in

    Getting started in Angular CLI projects

Relationship to older Angular Model Pattern and

This is a new enhanced version of older library called

which was in turn implementation of Angular Model Pattern. All the original examples and documentation are still valid. The only difference is that you can add
ng add
instead of installing
or having to copy model pattern implementation to your project manually.

One of the changes compared to

is that the
uses new
providedIn: 'root'
syntax (since Angular 6) so that we don't need to import
or anything similar to register
into Angular dependency injection (DI) context. All we have to do is to import
in the constructor of at least one service in our application like this
constructor(private modelFactory: ModelFactory) {}
and we're good to go. This new feature is called tree-shakeable providers:

There is now a new, recommended, way to register a provider, directly inside the @Injectable() decorator, using the new providedIn attribute. It accepts 'root' as a value or any module of your application. When you use 'root', your injectable will be registered as a singleton in the application, and you don’t need to add it to the providers of the root module. Similarly, if you use providedIn: UsersModule, the injectable is registered as a provider of the UsersModule without adding it to the providers of the module (source)

Migration from

Migration should be rather simple, please follow these steps:

  1. remove dependency to
  2. install
    npm i -S @angular-extensions/model
  3. remove
    from imports of your
    module based on where you added it
  4. search and replace imports from
    and replace them with
  5. this should be it! The API didn't change so this should be all that it takes to migrate to the new package
  6. (optional) refactor your existing model services to use
    @Injectable({ providedIn: 'root' })
    (or other module instead of
    ) instead of
    and remove them from
    @NgModule({ providers: [MyModelService] })
    of your modules

The 6th step depends on your preferences, it is still possible to use new

with classic providers, just use
flag when generating new model services using schematics


Please, feel free to submit and issue or feature request using project GitHub repository.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.