Socket
Socket
Sign inDemoInstall

ngx-inject-control

Package Overview
Dependencies
8
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    ngx-inject-control

Simple, reusable form components in Angular


Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

Ngx-Inject-Control

Build Test Lint

A painless and idiomatic way to create reusable form controls for Angular applications.

Battle-tested at Ezfire.

Version

  • For Angular >=12, please use ngx-inject-control 1.x.x. npm install ngx-inject-control@1.0.0

Installation

npm:

npm install --save ngx-inject-control

Yarn

yarn add ngx-inject-control

Features

  • Reusable form components
  • Idiomatic API
  • Behaves like you expect
  • Plays nicely with UI libraries (e.g. Angular Material)

Usage

Create an injectable control

To use ngx-inject-control you first need to create an InjectableControl. An InjectableControl is an Angular component with a property called control of type AbstractControl. Finally, the InjectableControl must provide itself with the NGX_INJECTABLE_CONTROL token.

For example, suppose you want to create a single email input you can use across you application.

Markup:
<div class="email-container">
  <label for="email-input">Email</label>
  <input id="email-input" name="email" type="email" [formControl]="control" />
</div>
Component:
import {
  InjectableControl,
  injectableControlProvider,
} from 'ngx-inject-control';

@Component({
  selector: 'app-email-input',
  templateUrl: '...',
  providers: [injectableControlProvider(EmailInputComponent)],
})
export class EmailInputComponent implements InjectableControl {
    constructor(private readonly fb: FormBuilder) {}

    readonly control = this.fb.control(null, [Validators.email]).
}

Using an injectable control

Module:

InjectControlNameDirective is a standalone component and can by imported directly in a module or component.

import { InjectControlNameDirective } from 'ngx-inject-control';


@NgModule({
  declarations: [EmailInputComponent], // Injectable component must be in scope
  imports: [InjectControlNameDirective],
})
Component:
<form [formGroup]="group" (ngSubmit)="onSubmit()">
  <input name="name" type="text" formControlName="name" />
  <app-email-input injectControlName="email"></app-email-input>
  <button>Submit</button>
</form>
@Component({
  selector: 'app-form',
  templateUrl: '...',
})
export class FormComponent {
  constructor(private readonly fb: FormBuilder) {}

  readonly group = this.fb.group({
    name: [null, Validators.required],
    email: [],
  });
}

On render, the injectControlName directive will replace the email field in group with the control field of EmailInputComponent. Validations are recomputed at the time of replacement.

API

InjectControlNameDirective
InputTypeDescription
injectControlNamestring or numberThe field or index to replace in the parent control.
disablebooleanEnable or disable the injected control.

License

MIT

Keywords

FAQs

Last updated on 08 Jun 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc