Skip to content

Helper Files

To reduce the amount of boilerplate needed with each component and to improve maintainability, you can set up a few helper objects. This way, should anything change, you only need to update one file.

You can place the helper files in any folder in your project. If you put them in the default location (app/shared/helper), they can be discovered automatically.

Otherwise, run schematics with the --hostDirectiveHelperPath and/or --viewProviderHelperPath flag to point to your files or configure them as defaults. The configuration for each schematic is described on the schematics pages Generators and Register.

ControlContainer is required for all controls and groups that will be used within ngx-formwork. Injection of the control container allows the components to use reactive forms functionality, without needing to pass the form group through inputs and wrapping the template into additional tags. See this YouTube Video for more detailed explanation: How to Make Forms in Angular REUSABLE (Advanced, 2023)

control-container.view-provider.ts
export const controlContainerViewProviders = [
{
provide: ControlContainer,
useFactory: () => inject(ControlContainer, { skipSelf: true }),
},
];
text-control.component.ts || group.component.ts
@Component({
// Other component decorator options
viewProviders: controlContainerViewProviders,
})

This is a convenience helper to apply the NgxfwControlDirective.

control.host-directive.ts
export const ngxfwControlHostDirective = {
directive: NgxfwControlDirective,
inputs: ['content', 'name'],
};

Use it like this:

text-control.component.ts
@Component({
// Other component decorator options
hostDirectives: [
// Apply here
ngxfwControlHostDirective
],
})

This is a convenience helper to apply the NgxfwGroupDirective.

group.host-directive.ts
export const ngxfwGroupHostDirective = {
directive: NgxfwGroupDirective,
inputs: ['content', 'name'],
};

Use it like this:

group.component.ts
@Component({
// Other component decorator options
hostDirectives: [
// Apply here
ngxfwGroupHostDirective
],
})

For official documentation of Union Types checkout the official docs.

Setting up a union type for your own controls is highly recommended, as it gives you much better type safety, when writing your forms in TypeScript.

export type MyAppControls = TestTextControl | TestGroup | InfoBlock;