Passing a FormGroup from an Angular 2 component as a parameter

I have a FormGroup that contains a SubFormGroup:

sub formGroup initialize:
    this.fg=new FormGroup({
        name: new FormControl(),
        abcFg: new FormGroup({
            aaa: new FormControl(),
            bbb: new FormControl()
        })
    });

In my main component, I have an ABCComponent:

@Component({
    selector: 'mainComponent',
    styles: [],
    providers: [],
    template: `
         <div *ngIf="fg">
             <form [formGroup]="fg">
                  <abcComponent></abcComponent>
             </form>
         </div>
         `

I want to pass the sub FormGroup (abc) to the ABCComponent as a parameter(input).

ABCComponent:

@Component({
    selector: 'abcComponent',
    styles: [],
    providers: [],
    template: `
         <div *ngIf="abcFg">
             <form [formGroup]="abcFg">
                 <input type="text" formControlName="aaa" />
                 <input type="text" formControlName="bbb" />
             </form>
         </div>
         `
export class AbcComponent{ 
    @input() public abcFg:FormGroup;
}

I have attempted to do this:

<abcComponent [abcFg]="fg.controls[abcFg"]"></abcComponent>

However, it is not working... How can I fix it? Thank you!

Answer №1

If you want to transfer the value of xyzValue to your xyzComponent, you can achieve it by following these steps:

<xyzComponent [xyzValue]="value.xyzValue"></xyzComponent>

Next, in your xyzComponent:

export class XyzComponent implements OnInit{ 
    @input() public xyzValue;
    xyzFormGroup : FormGroup;

    constructor(private formBuilder: FormBuilder){}

    ngOnInit(){
        this.xyzFormGroup = this.formBuilder.group({
            field1: [this.xyzValue.field1],
            field2: [this.xyzValue.field2]
        });
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Experiencing issues when running `ng serve` with a basic Angular CLI project created using

Just started using angular-cli and initiated an angular 2 project with the command ng new angular2. However, upon trying to run it using ng serve, I encounter crashes accompanied by errors. Error Log: ERROR in [default] C:\Users\user\deskt ...

Error occurs when attempting to call Angular from NodeJS

I am trying to integrate Angular with Node.js, but I'm encountering an error while accessing it in the browser. Angular works fine when I run "node server." Error: runtime.js:1 Uncaught SyntaxError: Unexpected token < polyfills.js:1 Uncaught Synt ...

The AgGridModule type does not feature the property 'ɵmod'

Recently, I decided to update my application from Angular 12 to Angular 13. The tools I am using include webpack 5, ag-grid 15.0.0, and ag-grid-angular 15.0.0. While the compilation process goes smoothly for the app, I encountered an issue when trying to l ...

Issue with narrowing TypeScript arrays often encountered

When working with arrays of strings in my TypeScript code, I restrict the contents to certain letters by using a defined type like ("A" | "B")[] for letters such as A and B. However, when I have a function that takes an arbitrary array ...

Validation issue with Reactive Forms not functioning as expected

My latest project involves a user signup component that I created from scratch import { Component } from '@angular/core'; import {UserManagementService} from '../user-management.service'; import {User} from "../user"; import {FormBuild ...

Fetch a JSON list containing missing objects and substitute them with null values using Angular 2

Here's my JSON array : "user" : { "id": 456, "nickname": "xxx", "pf": { "id": 123, "account": [ { "accountid": 1494235749, "status": "New", "accountnbr": 12345, ...

The Void Ionic type does not have the specified property available

Struggling to load markers when the button is clicked to find nearby gyms. Despite loading the map to my location upon click, no markers are displayed and an error indicating that then does not exist on type 'void' within IonViewDidLoad. gyms.ht ...

Generating Angular component based on selector

I developed an app that showcases various visualizations of RxJS operators such as rx-map, rx-filter, rx-reduce, and more. The visualizations function correctly on their own. Now, I am looking to enable users to select which visualization they want to view ...

What is the reason behind hidden DOM elements appearing when I refresh the page?

When I refresh my webpage, I notice that the DOM elements I have hidden using ngIf are briefly visible before the actual state of my webpage loads. Why might this be happening? I am currently using Angular 8 version. <div *ngIf="!callInProgress ...

Is there a way to incorporate a button with row span in ag-grid within an Angular project?

In my project, I have incorporated ag-grid. I successfully implemented a cell renderer to display a button in the "action" column. The button appears on each row and functions properly when clicked. Now, for my second task, I am looking to include a butto ...

Removing scrollbar from table in React using Material UI

I successfully created a basic table using react and material UI by following the instructions found at: https://material-ui.com/components/tables/#table. The table is functioning properly, but I am finding the scrollbar to be a bit inconvenient. https:// ...

Organize rows in the table while maintaining reactivity

A challenge I'm facing with a web app (Angular SPA) is that it displays a large table without the ability to sort. To work around this issue, I've managed to implement sorting via the console. However, re-inserting the rows after sorting causes t ...

The success method in the observable is failing to trigger

Could someone explain why the () success method is not triggering? It seems to be working fine when using forkjoin(). Shouldn't the success method fire every time, similar to a final method in a try-catch block? Note: Inline comments should also be c ...

Necessary CSS selector input equivalent in Reactive Forms

Is there a similar CSS method like input:required {CSS} to style all the reactive form fields in Angular that are set as required using Validators.required in their formControl? Or is there another approach to achieve this with CSS? ...

Why is this chip-list not functioning properly in my Angular 14 and Angular material application?

I'm currently developing a form using Angular 14. My goal is to incorporate a field with Angular Material chips. Within the component's Typescript file, I have the following code: constructor(private fb: FormBuilder,) { } public visible: boole ...

tRPC asynchronous mutation does not have a return value

Attempting to utilize tRPC for the first time here. I have created a mutation called "add" that takes a URL as input and returns a hardcoded slug. Router export const entryRouter = router({ add: publicProcedure .input(input) .output(output) ...

"Struggling with setting the default checked state for single and multiple checkboxes? The ng-init method with checkboxModel.value=true seems to be ineffective – any suggestions

<input type="checkbox" ng-model="isChecked.value" ng-true-value="'yes'" ng-false-value="'no'" ng-click='handleCheckboxChange(isChecked.value, idx);' ng-init="isChecked.value=true" /> ...

Protractor's compatibility with Headless Chrome is hindered on AWS CodeBuild, yet functions successfully when run locally

I am facing an issue with my webpage that requires Google Authentication before moving on to an angular web page. I have created some basic end-to-end tests which are working perfectly fine in Linux using Chrome Headless: The test finds the username fiel ...

In Typescript, is it correct to say that { [key: string]: any } is identical to { [key: number]: any }?

Recently diving into Typescript has been an interesting journey, especially when stumbling upon weird language behaviors. After writing the following code snippet, I was surprised to see that it compiled and executed successfully: let x: { [key: string]: a ...

How to add a class to an element using an Angular 2+ directive

I am working with an Angular 6.0.0 application and I have the requirement to dynamically add classes to HTML elements. My current implementation is functional, but I want to enhance it by making the prefix reusable through a directive. <div class="clas ...