npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ngx-dom-component

v1.2.2

Published

Angular 5+ component injector and manipulation tools

Downloads

247

Readme

NGX-DOM-Component

Provide component injection and manipulation in DOM for your angular 5+ application :

Allow you to inject component into the DOM including inputs/outputs. And then manipulating this component by removing/deleting it from the DOM.

npm i ngx-dom-component --save

NgxDOMComponentModule

Import NgxDOMComponentModule into your root module.

Example

import { Component, OnInit, ViewContainerRef, ViewChild } from '@angular/core'
import { NgxDOMComponentService, NgxDOMComponentCreateOptions, NgxDOMComponent } from 'ngx-dom-component';

@Component({
  selector: 'my-component',
  template: '<span>{{ message }}</span>'
})
export class MyComponent {
    @Input()
    public message;
}

@Component({
  selector: 'my-app',
  template: `
    <div class="container">
      <ng-template #contentContainer></ng-template>
    </div>
  `
})
export class AppComponent implements OnInit {
  @ViewChild('contentContainer', { read: ViewContainerRef })
  public contentContainerRef: ViewContainerRef;

  constructor(private ngxDOMComponentService: NgxDOMComponentService) {}

  ngOnInit() {
    this.ngxDOMComponentService.init(); // DO NOT FORGET TO INIT !!!!
    let child: NgxDOMComponent  = this.ngxDOMComponentService.create({
      viewContainerRef: contentContainerRef,
      componentType: MyComponent,
      inputs: {
        message: 'Hello World!'
      }
    });

    setTimeout(() => {
      child.destroy();
    }, 5000);
  }
}

NgxDOMComponentOptions

interface NgxDOMComponentOptions {
  viewContainerRef?: ViewContainerRef; // the parent of the component
  componentType: any; // the component class
  inputs?: { [key: string] : any }; // a list of inputs to provide to the component @Input
  outputs?: { [key: string] : Function }; // a list of output callbacks to provide to link with the component @Output
  index?: number; // the position where to inject your component
}

NgxDOMComponentService

Into your app.component.ts file, you must include the NgxDOMComponentService and then call init, or at least before any calls to the ngx-dom-component methods/constructor, etc...

createContainer(viewContainerRef: ViewContainerRef): NgxDOMComponentContainer;

Create a new NgxDOMComponentContainer.

createComponent(options: NgxDOMComponentCreateOptions): NgxDOMComponent;

Create a new NgxDOMComponent and inject it onto the dom.

NgxEntryComponent (Decorator)

In some case (ex: when compiling in AOT mode) the metadata properties are lost and the binding of the inputs/outputs becomes impossible. As a result you wont see your inputs changes (ngOnChanges) and the outputs wont be binded. If you encounter this situation, you can use the decorator NgxEntryComponent and provide directly the list of properties as inputs and outputs

@NgxEntryComponent({
  inputs: ['message']
  outputs: []
})
@Component({
  selector: 'my-component',
  template: 'some html'
})
export class MyComponent {
    @Input()
    public message: string;
    //...
}

NgxDOMComponentContainer

Create a wrapper for the ViewContainerRef, allowing you not providing the viewContainerRef attribute of NgxDOMComponentCreateOptions.

class NgxDOMComponentContainer {
  viewContainerRef: ViewContainerRef;
  constructor(viewContainerRef: ViewContainerRef);
  create(options: NgxDOMComponentCreateOptions): NgxDOMComponent;
}

Example:

this.ngxDOMComponentContainer = new NgxDOMComponentContainer(this.contentContainerRef);
this.ngxDOMComponentContainer.create({
  componentType: MyComponent,
  inputs: {
    message: 'Hello World!'
  }
});

NgxDOMComponent

Create and inject a Angular 5+ Component into the DOM.

class NgxDOMComponent {
  constructor(options: NgxDOMComponentCreateOptions);

  readonly viewContainerRef: ViewContainerRef; // the parent container
  readonly componentRef: ComponentRef<any>; // the component ref
  readonly componentType: any; // the component class
  readonly element: HTMLElement; // the dom element
  readonly instance: any; // the instance of the constructor

  insert(index?: number, viewContainerRef?: ViewContainerRef): this; // insert the component at a specific index in viewContainerRef
  move(index?: number, viewContainerRef?: ViewContainerRef): this;  // move the component at a specific index in viewContainerRef
  detach(): this; // detach the component from the dom
  destroy(): this; // destroy the component

  detectChanges(): void; // manually trigger a changes detection, calling ngOnChanges if some elements changed
}

NgxDOMComponentComponent

<ngx-dom-component
  [config]="{
    component: myComponent,
    inputs: {
      message: 'Hello World!'
    }
  }"
></ngx-dom-component>

Inject an angular 2+ component. The config is the same as NgxDOMComponentOptions except that you don't need to provide a container.