Detalhes do pacote

@ngneat/overview

ngneat127kMIT6.1.2

The Template for Success in Template Management

angular, views, dynamic views, teleport

readme (leia-me)


npm MIT Commitizen friendly PRs styled with prettier All Contributors ngneat spectator

Overview - The Template for Success in Template Management

Compatibility with Angular Versions

@ngneat/overview Angular
6.x >= 17
5.x >= 16
4.x >= 14 < 16
3.x >= 13 < 14
2.x >= 11 < 13

Installation

npm i @ngneat/overview
yarn add @ngneat/overview

Table of Contents

DynamicView

Use the dynamicView structural directive to render a component, a template, HTML, or default template dynamically.

Let’s say we build a generic error component. What we want is to give our consumers the flexibly to create it by using one of three options:

  • They can choose to use the default template
  • They can choose to use their own text which can be static or dynamic
  • They can choose to pass their own template or component
import { DynamicViewDirective, Content } from '@ngneat/overview';

@Component({
  template: ` <div *dynamicView="view">Default view</div> `,
  standalone: true,
  imports: [DynamicViewDirective],
})
export class ErrorComponent {
  @Input() view: Content | undefined;
}

You can also pass a context or an injector as inputs to the directive:

<h5>Component</h5>
<ng-container
  *dynamicView="component; 
    injector: myInjector; 
    context: { $implicit: 'my title' }"
/>

<h5>Template</h5>
<ng-template #tpl let-title>
  <b>{{ title }}</b>
</ng-template>

<ng-container *dynamicView="tpl; context: { $implicit: 'my title' }" />

If you pass context to a component and the value can be accessed via the injectViewContext function:

import { injectViewContext } from '@ngneat/overview';

interface Context {
  title: string;
}

@Component({
  template: `<div>{{ context().title }}</div>`,
  standalone: true,
})
export class MyDynamicComponent {
  context: Signal<Context> = injectViewContext<Context>();
}

injectViewContext returns a readonly signal with the view's context object.

Teleporting

Teleporting means rendering a view at a different location from its declaration. There are two cases it might be helpful:

  • Avoid prop drilling to a nested component.
  • Rendering a view at another place in the DOM while keeping its context where it’s defined.

You can read more about this approach in this article.

Use the teleportOutlet directive to define a new outlet. An outlet is an anchor where the view will be projected as a sibling.

import { TeleportOutletDirective } from '@ngneat/overview';

@Component({
  template: `
    <div class="flex">
      <ng-container teleportOutlet="someId"/>
    </div>
  `,
  standalone: true,
  imports: [TeleportOutletDirective],
})
export class FooComponent {}

Use the teleportTo directive to teleport the view to a specific outlet:

import { TeleportDirective } from '@ngneat/overview';

@Component({
  template: `
    <section *teleportTo="someId">
      {{ value }}
    </section>
  `,
  standalone: true,
  imports: [TeleportDirective],
})
export class BarComponent {
  value = '...';
}

ViewService

The ViewService provides facade methods to create modular views in Angular. It's been used in various projects like hot-toast, and helipopper.

createComponent

The createComponent method takes a Component, and returns an instance of CompRef:

import { ViewService, CompRef } from '@ngneat/overview';

@Injectable()
class ToastService {
  private viewService = inject(ViewService);
  componentRef: CompRef;

  init() {
    this.componentRef = this.viewService
      .createComponent(HotToastContainerComponent)
      .setInput('defaultConfig', defaultConfig)
      .appendTo(document.body);
  }
}

There are cases where we want to use an Angular component template in a third-party library that takes a native DOM element or a string. In this case, we can use the getRawContent or the getElement method, respectively.

import { ViewService } from '@ngneat/overview';

@Directive()
class ChartDirective {
  private viewService = inject(ViewService);

  createChart(color: string) {
    const ref = this.viewService.createComponent(FooTooltip).setInput('color', color).detectChanges(document.body);

    const content = ref.getRawContent();

    ref.destroy();

    Highcharts.chart('container', {
      tooltip: {
        formatter: function () {
          return content;
        },
        useHTML: true,
      },
    });
  }
}

createComponent Options

createComponent<Comp, Context>(component: Type<Comp>, {
  injector?: Injector,
  environmentInjector?: EnvironmentInjector,
  context?: Context,
  vcr?: ViewContainerRef,
})

createTemplate

The createTemplate method takes a TemplateRef, and returns an instance of ViewRef.

createTemplate<Context>(tpl: TemplateRef<Context>, {
  context?: Context,
  vcr?: ViewContainerRef,
  injector?: Injector,
})

createView

The createView method takes a Component, a TemplateRef or a string, and creates a View:

import { ViewService, Content } from '@ngneat/overview';

@Injectable()
class ToastService {
  private viewService = inject(ViewService);

  createToast(content: Content) {
    const ref = this.viewService.createView(content);
    document.body.appendChild(ref.getElement());
  }
}

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Netanel Basal

💻 🤔

Dharmen Shah

🖋 📖

This project follows the all-contributors specification. Contributions of any kind welcome!

Icons made by Freepik from www.flaticon.com

changelog (log de mudanças)

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

6.1.2 (2025-03-31)

6.1.1 (2024-08-06)

Bug Fixes

  • use ngOnChanges instead of effect to avoid signal-writes error (e8c3d3c)

6.1.0 (2024-07-03)

Features

  • change to signal inputs to be zoneless compatible (2965bda)

6.0.0 (2023-11-28)

⚠ BREAKING CHANGES

  • 🧨 Peer deps are now angular 17 and everything is standalone

Features

5.1.1 (2023-07-05)

Bug Fixes

5.1.0 (2023-07-05)

Features

5.0.0 (2023-05-29)

⚠ BREAKING CHANGES

  • 🧨 The context passed to a component is now a signal
  • 🧨 peer dependency is now angular v16+

Features

  • 🎸 use signal to provide component context (2b8aebb)

Bug Fixes

  • 🐛 update view on context change (5f38a78)
  • 🤖 update to angular v16 (488187c)

4.1.0 (2023-02-09)

Features

  • 🎸 expose ref for templates (0d848b1)
  • add injector option to the template config (229e725), closes #17
  • add injector option to the template config (d37c6e2), closes #17

4.0.1 (2023-01-28)

Bug Fixes

  • 🐛 pass injector to component ref (a9d5217)

4.0.0 (2023-01-22)

⚠ BREAKING CHANGES

  • 🧨 build target is now es2022, peer dependency is now angular v14+
  • 🧨 build target is now es2020

Features

  • 🎸 support context in components as injection token (22b8ae0)
  • 🎸 update to angular v14 (448f33d)
  • 🎸 update to angular v15 (ff8ec48)

3.0.4 (2022-01-30)

Bug Fixes

3.0.3 (2022-01-27)

Bug Fixes

  • 🐛 component should run cd (ebfeda5)

3.0.2 (2021-12-22)

Bug Fixes

  • 🐛 resolve view ref type (a041419)

3.0.1 (2021-12-22)

Bug Fixes

  • 🐛 create view typings (5df4a42)

3.0.0 (2021-11-16)

⚠ BREAKING CHANGES

  • The @ngneat/overview is shipped with .mjs files, following the APF (Angular Package Format) spec starting from Angular 13. .mjs files are compatible only with Angular 13 version and higher.

Features

  • upgrade to Angular 13 and switch to modern APF (33f7463)
  • use new createComponent signature (d47e249)

2.1.0 (2021-10-25)

Features

  • allow teleport bindings to be passed asynchronously (05c164f)

2.0.4 (2021-08-10)

Bug Fixes

  • 🐛 use the directive injector if not provided (dda0a96)

2.0.3 (2021-08-09)

Bug Fixes

2.0.2 (2021-06-02)

Bug Fixes

  • 🐛 revert to view engine (ffadce2)

2.0.1 (2021-05-28)

⚠ BREAKING CHANGES

  • lib: remove dynamic-content and tplOrString

remove dynamic-content and tplOrString in favor of the dynamicView directive

Features

  • lib: add dynamicView directive (b0b43d4)

Bug Fixes

  • 🐛 detect dynamic content changes (046277a)

2.0.0 (2021-03-29)

⚠ BREAKING CHANGES

  • lib: remove dynamic-content and tplOrString

remove dynamic-content and tplOrString in favor of the dynamicView directive

Features

  • lib: add dynamicView directive (b0b43d4)

1.0.0 (2021-01-28)

Features

  • 🎸 add contect and provider (8fa76e4)

Bug Fixes