All Projects → gund → Ng Dynamic Component

gund / Ng Dynamic Component

Licence: mit
Dynamic components with full life-cycle support for inputs and outputs for Angular

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Ng Dynamic Component

Vue Dynamic Form Component
Vue dynamic nested form component, support nested Object/Hashmap/Array. Vue动态多级表单组件,支持嵌套对象/Hashmap/数组。
Stars: ✭ 220 (-44.44%)
Mutual labels:  dynamic, component
Reason Loadable
🔥 Suspense/Lazy for ReasonReact.
Stars: ✭ 88 (-77.78%)
Mutual labels:  dynamic, component
Mvvmhabitcomponent
👕基于MVVMHabit框架,结合阿里ARouter打造的一套Android MVVM组件化开发方案
Stars: ✭ 857 (+116.41%)
Mutual labels:  lifecycle, component
React Native Wormhole
⚛️ 🌌 Inter-dimensional Portals for React Native. 👽 🖖
Stars: ✭ 133 (-66.41%)
Mutual labels:  dynamic, component
Mvvmframe
🏰 MVVMFrame for Android 是一个基于Google官方推出的Architecture Components dependencies(现在叫JetPack){ Lifecycle,LiveData,ViewModel,Room } 构建的快速开发框架。有了MVVMFrame的加持,从此构建一个MVVM模式的项目变得快捷简单。
Stars: ✭ 218 (-44.95%)
Mutual labels:  lifecycle, component
Grouter Android
原名ActivityRouter,Android 页面及服务组件化框架
Stars: ✭ 375 (-5.3%)
Mutual labels:  component
Walkable
A Clojure(script) SQL library for building APIs: Datomic® (GraphQL-ish) pull syntax, data driven configuration, dynamic filtering with relations in mind
Stars: ✭ 384 (-3.03%)
Mutual labels:  dynamic
Miox
Modern infrastructure of complex SPA
Stars: ✭ 374 (-5.56%)
Mutual labels:  lifecycle
C Cpp Notes
Notes about modern C++, C++11, C++14 and C++17, Boost Libraries, ABI, foreign function interface and reference cards.
Stars: ✭ 363 (-8.33%)
Mutual labels:  dynamic
React Swipeable Views
A React component for swipeable views. ❄️
Stars: ✭ 4,095 (+934.09%)
Mutual labels:  component
React Scrolllock
🔒 Prevent scroll on the <body />
Stars: ✭ 393 (-0.76%)
Mutual labels:  component
Dynamic Kg
Dynamic (Temporal) Knowledge Graph Completion (Reasoning)
Stars: ✭ 381 (-3.79%)
Mutual labels:  dynamic
Vue Tagsinput
A simple tags input with typeahead (autocomplete) built with Vue.js 2.
Stars: ✭ 375 (-5.3%)
Mutual labels:  component
Vue Simple Spinner
A simple, flexible spinner for Vue.js
Stars: ✭ 385 (-2.78%)
Mutual labels:  component
Element Queries Spec
A spec for a Container-Style Element Query Syntax
Stars: ✭ 375 (-5.3%)
Mutual labels:  component
Grassbending
A replacement for Unity's terrain grass shader with alpha blended rendering and touch bending effect
Stars: ✭ 397 (+0.25%)
Mutual labels:  dynamic
Polyfill Ctype
This component provides a partial, native PHP implementation for the Ctype extension.
Stars: ✭ 3,774 (+853.03%)
Mutual labels:  component
React Select Search
⚡️ Lightweight select component for React
Stars: ✭ 379 (-4.29%)
Mutual labels:  component
Angular Datepicker
Highly configurable date picker built for Angular applications
Stars: ✭ 386 (-2.53%)
Mutual labels:  component
React Scrollspy
🔯 react scrollspy component
Stars: ✭ 382 (-3.54%)
Mutual labels:  component

ng-dynamic-component

Dynamic components with full life-cycle support for inputs and outputs

Travis CI Appveyor Coverage Maintainability Npm Npm Downloads Licence semantic-release Greenkeeper badge

Angular ng-dynamic-component NPM package
11.x.x 8.x.x [email protected]^8.0.0
10.x.x 7.x.x [email protected]^7.0.0
9.x.x 6.x.x [email protected]^6.0.0
8.x.x 5.x.x [email protected]^5.0.0
7.x.x 4.x.x [email protected]^4.0.0
6.x.x 3.x.x [email protected]^3.0.0
5.x.x 2.x.x [email protected]^2.0.0
4.x.x 1.x.x [email protected]^1.0.0
2.x.x 0.x.x [email protected]^0.0.0

Installation

$ npm install ng-dynamic-component --save

Error message in the IDE

If you have an error like Can't bind to 'ndcDynamicInputs' since it isn't a known property of 'ndc-dynamic' in your IDE, but the project compiles just fine, you might want to try installing the no-barrels version instead.

$ npm install --save [email protected]

Usage

DynamicComponent

Import DynamicModule where you need to render dynamic components:

import { DynamicModule } from 'ng-dynamic-component';

@NgModule({
  imports: [DynamicModule],
})
export class MyModule {}

Then in your component's template include <ndc-dynamic> where you want to render component and bind from your component class type of component to render:

@Component({
  selector: 'my-component',
  template: `
    <ndc-dynamic [ndcDynamicComponent]="component"></ndc-dynamic>
  `,
})
class MyComponent {
  component = Math.random() > 0.5 ? MyDynamicComponent1 : MyDynamicComponent2;
}

NgComponentOutlet

You can also use NgComponentOutlet directive from @angular/common instead of <ndc-dynamic>.

Import DynamicIoModule where you need to render dynamic inputs:

import { DynamicIoModule } from 'ng-dynamic-component';

@NgModule({
  imports: [DynamicIoModule],
})
export class MyModule {}

Now apply ndcDynamicInputs and ndcDynamicOutputs to ngComponentOutlet:

@Component({
  selector: 'my-component',
  template: `<ng-template [ngComponentOutlet]="component"
                           [ndcDynamicInputs]="inputs"
                           [ndcDynamicOutputs]="outputs"
                           ></ng-template>`
})
class MyComponent {
  component = MyDynamicComponent1;
  inputs = {...};
  outputs = {...};
}

Also you can use ngComponentOutlet with * syntax:

@Component({
  selector: 'my-component',
  template: `<ng-container *ngComponentOutlet="component;
                            ndcDynamicInputs: inputs;
                            ndcDynamicOutputs: outputs"
                            ></ng-container>`
})
class MyComponent {
  component = MyDynamicComponent1;
  inputs = {...};
  outputs = {...};
}

Inputs and Outputs

You can pass inputs and outputs to your dynamic components:

Import module DynamicIoModule and then in template:

@Component({
  selector: 'my-component',
  template: `
    <ndc-dynamic
      [ndcDynamicComponent]="component"
      [ndcDynamicInputs]="inputs"
      [ndcDynamicOutputs]="outputs"
    ></ndc-dynamic>
  `,
})
class MyComponent {
  component = MyDynamicComponent1;
  inputs = {
    hello: 'world',
    something: () => 'can be really complex',
  };
  outputs = {
    onSomething: type => alert(type),
  };
}

@Component({
  selector: 'my-dynamic-component1',
  template: 'Dynamic Component 1',
})
class MyDynamicComponent1 {
  @Input()
  hello: string;
  @Input()
  something: Function;
  @Output()
  onSomething = new EventEmitter<string>();
}

Here you can update your inputs (ex. inputs.hello = 'WORLD') and they will trigger standard Angular's life-cycle hooks (of course you should consider which change detection strategy you are using).

Output template variables

Since v6.1.0

When you want to provide some values to your output handlers from template - you can do so by supplying a special object to your output that has shape {handler: fn, args: []}:

@Component({
  selector: 'my-component',
  template: `
    <ndc-dynamic
      [ndcDynamicComponent]="component"
      [ndcDynamicOutputs]="{
        onSomething: { handler: doSomething, args: ['$event', tplVar] }
      }"
    ></ndc-dynamic>
  `,
})
class MyComponent {
  component = MyDynamicComponent1;
  tplVar = 'some value';
  doSomething(event, tplValue) {}
}

Here you can specify at which argument event value should arrive via '$event' literal.

HINT: You can override event literal by providing EventArgumentToken in DI.

Component Creation Events

You can subscribe to component creation events, being passed a reference to the ComponentRef:

@Component({
  selector: 'my-component',
  template: `
    <ndc-dynamic
      [ndcDynamicComponent]="component"
      (ndcDynamicCreated)="componentCreated($event)"
    ></ndc-dynamic>
  `,
})
class MyComponent {
  component = MyDynamicComponent1;
  componentCreated(compRef: ComponentRef<any>) {
    // utilize compRef in some way ...
  }
}

Attributes

Since v2.2.0 you can now declaratively set attributes, as you would inputs, via ndcDynamicAttributes.

Import module DynamicAttributesModule and then in template:

import { AttributesMap } from 'ng-dynamic-component';

@Component({
  selector: 'my-component',
  template: `
    <ndc-dynamic
      [ndcDynamicComponent]="component"
      [ndcDynamicAttributes]="attrs"
    ></ndc-dynamic>
  `,
})
class MyComponent {
  component = MyDynamicComponent1;
  attrs: AttributesMap = {
    'my-attribute': 'attribute-value',
    class: 'some classes',
  };
}

Remember that attributes values are always strings (while inputs can be any value). So to have better type safety you can use AttributesMap interface for your attributes maps.

Also you can use ngComponentOutlet and ndcDynamicAttributes with * syntax:

import { AttributesMap } from 'ng-dynamic-component';

@Component({
  selector: 'my-component',
  template: `
    <ng-container
      *ngComponentOutlet="component; ndcDynamicAttributes: attrs"
    ></ng-container>
  `,
})
class MyComponent {
  component = MyDynamicComponent1;
  attrs: AttributesMap = {
    'my-attribute': 'attribute-value',
    class: 'some classes',
  };
}

Directives (experimental)

Since v3.1.0 you can now declaratively set directives, via ndcDynamicDirectives.

NOTE: In dynamic directives queries like @ContentChild and host decorators like @HostBinding will not work due to involved complexity required to handle it (but PRs are welcome!).

Import module DynamicDirectivesModule and then in template:

import { dynamicDirectiveDef } from 'ng-dynamic-component';

@Component({
  selector: 'my-component',
  template: `
    <ng-container
      [ngComponentOutlet]="component"
      [ndcDynamicDirectives]="dirs"
    ></ng-container>
  `,
})
class MyComponent {
  component = MyDynamicComponent1;
  dirs = [dynamicDirectiveDef(MyDirective)];
}

It's also possible to bind inputs and outputs to every dynamic directive:

import { dynamicDirectiveDef } from 'ng-dynamic-component';

@Component({
  selector: 'my-component',
  template: `
    <ng-container
      [ngComponentOutlet]="component"
      [ndcDynamicDirectives]="dirs"
    ></ng-container>
  `,
})
class MyComponent {
  component = MyDynamicComponent1;
  directiveInputs = { prop1: 'value' };
  directiveOutputs = { output1: evt => this.doSomeStuff(evt) };
  dirs = [
    dynamicDirectiveDef(
      MyDirective,
      this.directiveInputs,
      this.directiveOutputs,
    ),
  ];
}

To change inputs, just update the object:

class MyComponent {
  updateDirectiveInput() {
    this.directiveInputs.prop1 = 'new value';
  }
}

You can have multiple directives applied to same dynamic component (only one directive by same type):

import { dynamicDirectiveDef } from 'ng-dynamic-component';

@Component({
  selector: 'my-component',
  template: `
    <ng-container
      [ngComponentOutlet]="component"
      [ndcDynamicDirectives]="dirs"
    ></ng-container>
  `,
})
class MyComponent {
  component = MyDynamicComponent1;
  dirs = [
    dynamicDirectiveDef(MyDirective1),
    dynamicDirectiveDef(MyDirective2),
    dynamicDirectiveDef(MyDirective3),
    dynamicDirectiveDef(MyDirective1), // This will be ignored because MyDirective1 already applied above
  ];
}

Extra

You can have more advanced stuff over your dynamically rendered components like setting custom injector ([ndcDynamicInjector]) or providing additional/overriding providers ([ndcDynamicProviders]) or both simultaneously or projecting nodes ([ndcDynamicContent]).

NOTE: In practice functionality of this library is split in two pieces:

  • one - component (ndc-dynamic) that is responsible for instantiating and rendering of dynamic components;
  • two - directive (ndcDynamic also bound to ndc-dynamic) that is responsible for carrying inputs/outputs to/from dynamic component by the help of so called DynamicComponentInjector.

Thanks to this separation you are able to connect inputs/outputs and life-cycle hooks to different mechanisms of injecting dynamic components by implementing DynamicComponentInjector and providing it via DynamicComponentInjectorToken in DI.

It was done to be able to reuse NgComponentOutlet added in Angular 4-beta.3.

To see example of how to implement custom component injector - see ComponentOutletInjectorDirective that is used to integrate NgComponentOutlet directive with inputs/outputs.

License

MIT © Alex Malkevich

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].