Detalhes do pacote

@ngxd/core

IndigoSoft24.8kMIT16.0.0

NGXD is a dynamic pattern + library for Angular

angular, angular5, angular6, angular-library

readme (leia-me)

✨🦊 NGX Dynamic for Angular Ivy and Angular 7, 8, 9, 10, 11, 12, 13, 14, 15, 16+

preview

🥳 Best way to quickly use Dynamic Components with Angular

Npm Npm Downloads Npm Downloads Weekly Travis Licence

GitHub Contributors GitHub PR GitHub Issue GitHub Stars

Use like NgComponentOutlet but with @Input and @Output auto bindings:

<ng-container *ngxComponentOutlet="component"></ng-container>

Here is a demo example showing NGX Dynamic and Angular in action.

Getting started

Step 1: Install @ngxd/core:

npm install --save @ngxd/core
# or
yarn add @ngxd/core

Note: @ngxd/core@12 only supports angular with Ivy is enabled

Note: If you want to use @ngxd/core with a specific angular version, you have to install @ngxd/core which version you need:

  • @angular/core@7 => npm install @ngxd/core@7
  • @angular/core@8 => npm install @ngxd/core@8
  • @angular/core@9 => npm install @ngxd/core@9
  • @angular/core@10 => npm install @ngxd/core@10
  • @angular/core@11 => npm install @ngxd/core@11
  • @angular/core@12 => npm install @ngxd/core@12
  • @angular/core@13 => npm install @ngxd/core@13
  • @angular/core@14 => npm install @ngxd/core@14
  • @angular/core@15 => npm install @ngxd/core@15
  • @angular/core@16 => npm install @ngxd/core@16

Step 2: Import the NgxdModule:

import { NgxdModule } from '@ngxd/core';

@NgModule({
  declarations: [ AppComponent ],
  // have import NgxdModule here 👇
  imports: [ BrowserModule, NgxdModule ],
  bootstrap: [ AppComponent ]
})
export class AppModule {}

Step 3: Use NgxComponentOutlet directly:

@Component({
  template: `
    <ng-container *ngxComponentOutlet="component"></ng-container>`
    // using @ngxd/core 👆
})
class MyComponent {  
  // your dynamic component 👇
  component = DynamicComponent;

  // 🥳 inputs and outputs will binding automatically
  @Input() entity;
  @Output() action;
}

Use cases

There are several modes of operation of the directive.

1. Binding inputs and outputs

A simple variant of binding through the parent component.

@Component({
  template: `
    <ng-container *ngxComponentOutlet="component"></ng-container>`
    // using @ngxd/core 👆
})
class MyComponent {  
  // your dynamic component 👇
  component = DynamicComponent;

  // 🥳 inputs and outputs will binding automatically
  @Input() entity;
  @Output() action;
}

Binding inputs through the context

Note: You not permitted to passing the outputs using the context. The context will be passing inputs only.

In the example below, you can see the binding through the context. This is useful when you need to display something through *ngFor. Note that the context has a higher priority than the inputs in the component.

<ng-container *ngFor="let color of colors"
  <ng-container *ngxComponentOutlet="
      component; context: { color: color }
  "></ng-container>
</ng-container>

2. Switching the component

To switch a component, you just need to overwrite it with another one.

class AppComponent {
  ngOnChanges(changes: SimpleChanges): void {
    if ('type' in changes) {
      switch (this.type) {
        case 'number':
          this.component = NumberComponent;
          break;
        case 'text':
          this.component = TextComponent;
          break;
        default:
          this.component = DefaultComponent;
      }
    }
  }
}

Switching the component using pipe and resolver

If you have a bunch of components, then you go to switch between them. To do this, you can use NgxdResolvePipe and NgxdResolver to help you isolate dynamic component selection.

<ng-container *ngxComponentOutlet="
    resolver | resolve : type
"></ng-container>

3. Lazy loading the dynamic component

If you need to load and display a dynamic component lazily, then you can use lazy import and pass it to the async pipe.

component = import('./my-lazy-component')
  .then(m => m.MyLazyComponent);
<ng-container *ngxComponentOutlet="
    component | async
"></ng-container>

Lazy loading bunch of dynamic components

You can also load a bunch of components lazily and render them.

resolver = import('./my-lazy-resolver')
  .then(m => m.myLazyResolver);
<ng-container *ngxComponentOutlet="
    resolver | async | resolve : type
"></ng-container>

4. Content projection

If you want to use the <ng-content> and pass the content to your dynamic component, you have to check the example below.

Click to here

Comparison

Feature NgxComponentOutlet ComponentFactoryResolver NgComponentOutlet
Friendliness ⭐⭐⭐ ⭐⭐
Dynamic Components
AOT support
Reactivity
Injector
NgModule
projectionNodes
Component Access
Lifecycle OnChanges ⭕️ manually
Binding @Input() ⭕️ manually
Binding @Output() ⭕️ manually
Activate Event ⭕️ manually
Deactivate Event ⭕️ manually

API

Input Type Default Required Description
[ngxComponentOutlet] Type<any> n/a yes
[ngxComponentOutletContext] any n/a no
[ngxComponentOutletInjector] Injector n/a no
[ngxComponentOutletContent] any[][] n/a no
Output Type Description
(ngxComponentOutletActivate) any
(ngxComponentOutletDeactivate) any

changelog (log de mudanças)

16.0.0 (2023-05-23)

Features

  • core: bump v16.0.0 and dropped @angular/core@15 and earlier, removing legacy code for view engine (d2fcdda)

BREAKING CHANGES

  • core: DROPPED @angular/core@15 AND EARLIER

If you want to use @ngxd/core with a specific angular version, you have to install @ngxd/core which version you need:

@angular/core@7 => npm install @ngxd/core@7 @angular/core@8 => npm install @ngxd/core@8 @angular/core@9 => npm install @ngxd/core@9 @angular/core@10 => npm install @ngxd/core@10 @angular/core@11 => npm install @ngxd/core@11 @angular/core@12 => npm install @ngxd/core@12 @angular/core@13 => npm install @ngxd/core@13 @angular/core@14 => npm install @ngxd/core@14 @angular/core@15 => npm install @ngxd/core@15 @angular/core@16 => npm install @ngxd/core@16

15.0.0 (2022-11-17)

Features

  • core: bump v15.0.0 and dropped @angular/core@14 and earlier (ec0523e)

BREAKING CHANGES

  • core: DROPPED ViewEngine

DROPPED @angular/core@14 AND EARLIER

If you want to use @ngxd/core with a specific angular version, you have to install @ngxd/core which version you need:

@angular/core@7 => npm install @ngxd/core@7 @angular/core@8 => npm install @ngxd/core@8 @angular/core@9 => npm install @ngxd/core@9 @angular/core@10 => npm install @ngxd/core@10 @angular/core@11 => npm install @ngxd/core@11 @angular/core@12 => npm install @ngxd/core@12 @angular/core@13 => npm install @ngxd/core@13 @angular/core@14 => npm install @ngxd/core@14 @angular/core@15 => npm install @ngxd/core@15

14.0.0 (2022-09-19)

Features

  • core: bump v14.0.0 and dropped @angular/core@13 and earlier (ce3d521)

BREAKING CHANGES

  • core: DROPPED @angular/core@13 AND EARLIER

    If you want to use @ngxd/core with a specific angular version, you have to install @ngxd/core which version you need:

    @angular/core@7  => npm install @ngxd/core@7
    @angular/core@8  => npm install @ngxd/core@8
    @angular/core@9  => npm install @ngxd/core@9
    @angular/core@10 => npm install @ngxd/core@10
    @angular/core@11 => npm install @ngxd/core@11
    @angular/core@12 => npm install @ngxd/core@12
    @angular/core@13 => npm install @ngxd/core@13
    @angular/core@14 => npm install @ngxd/core@14

13.0.0 (2022-01-27)

Features

  • core: bump v13.0.0 and dropped @angular/core@12 and earlier (b647da7)

BREAKING CHANGES

  • core: DROPPED ViewEngine

DROPPED @angular/core@12 AND EARLIER

If you want to use @ngxd/core with a specific angular version, you have to install @ngxd/core which version you need:

  @angular/core@7  => npm install @ngxd/core@7
  @angular/core@8  => npm install @ngxd/core@8
  @angular/core@9  => npm install @ngxd/core@9
  @angular/core@10 => npm install @ngxd/core@10
  @angular/core@11 => npm install @ngxd/core@11
  @angular/core@12 => npm install @ngxd/core@12
  @angular/core@13 => npm install @ngxd/core@13

12.0.1 (2021-09-28)

Bug Fixes

  • adding null to possible types to support async pipe (4a88e1c)

Features

  • core: bump v12.0.0 and dropped @angular/core@11 and earlier (eccb2c2)

BREAKING CHANGES

  • core: DROPPED ViewEngine

DROPPED @angular/core@11 AND EARLIER

If you want to use @ngxd/core with a specific angular version, you have to install @ngxd/core which version you need:

  @angular/core@7  => npm install @ngxd/core@7
  @angular/core@8  => npm install @ngxd/core@8
  @angular/core@9  => npm install @ngxd/core@9
  @angular/core@10 => npm install @ngxd/core@10
  @angular/core@11 => npm install @ngxd/core@11
  @angular/core@12 => npm install @ngxd/core@12

11.0.1 (2021-04-26)

11.0.0 (2021-03-23)

Features

  • core: bump v11.0.0 and dropped @angular/core@10 and earlier (f86056e)

BREAKING CHANGES

  • core: DROPPED @angular/core@10 AND EARLIER If you want to use @ngxd/core with a specific angular version, you have to install @ngxd/core which version you need:
    @angular/core@7  => npm install @ngxd/core@7
    @angular/core@8  => npm install @ngxd/core@8
    @angular/core@9  => npm install @ngxd/core@9
    @angular/core@10 => npm install @ngxd/core@10
    @angular/core@11 => npm install @ngxd/core@11

10.0.0 (2020-07-17)

Features

  • core: bump v10.0.0 and dropped @angular/core@9 and earlier (81147f4)

BREAKING CHANGES

  • core: DROPPED @angular/core@9 AND EARLIER If you whant to use @ngxd/core with specific angular version, please install @ngxd/core which version you need: @angular/core@7 => npm install @ngxd/core@7 @angular/core@8 => npm install @ngxd/core@8 @angular/core@9 => npm install @ngxd/core@9 @angular/core@10 => npm install @ngxd/core@10

9.0.4 (2020-02-20)

Bug Fixes

  • core: call setter on host component with host context (c8b5d1d), closes #21

9.0.3 (2020-02-19)

Bug Fixes

  • core: made host input descriptor as configurable (8dda070), closes #26 #29

9.0.2 (2020-02-19)

Bug Fixes

  • disabled Ivy for library, libs should use ViewEngine compiler (5ec5c79), closes #28

9.0.1 (2020-02-18)

Bug Fixes

Features

  • up angular version to 9 (04b4789)

BREAKING CHANGES

  • core: Components with getter and setter without both are not supported

9.0.0 (2020-02-07)

Bug Fixes

  • hide angular v9 warning (77598e9)

8.0.0 (2019-05-29)

7.0.3 (2019-04-03)

Bug Fixes

7.0.2 (2019-04-02)

Bug Fixes

  • core: broken tests (#9) (0b223fe)
  • core: store output subscriptions and pass value to setter (e4f2d7d)

7.0.1 (2019-01-22)

7.0.0 (2018-11-08)

Bug Fixes

  • update to angular 7, silence warning (278b6d2), closes #6
  • update to angular 7, silence warning (fd32c40)