Utilizing NgModelGroup nesting in various components for improved data management

Whenever I attempt to nest NgModelGroup inside another NgModelGroup, Angular seems to just ignore it. I'm currently utilizing Angular 12 and Template-driven Forms.

Here is how my code appears:

app.component.html

<form #form="ngForm">
  <div class="col-auto">
    <input name="name" id="name" ngModel /><br />
    <app-address></app-address>
  </div>
  <div class="btn-toolbar">
    <button type="submit" (click)="submit(form)">Submit</button>
  </div>
  <p>{{ form.value | json }}</p>
</form>

address.component.html

<div ngModelGroup="address">
  <input name="street" id="street" ngModel />
  <app-contact></app-contact>
</div>

contact.component.html

<div ngModelGroup="contact">
  <input name="phone" id="phone" ngModel />
</div>

This is the current output:

{
   "name":"John Snow",
   "address":{
      "street":"201 Somewhere Place, 12"
   },
   "contact":{
      "phone":"6405106300"
   }
}

And this is the desired outcome that I'm struggling to achieve:

{
   "name":"John Snow",
   "address":{
      "street":"201 Somewhere Place, 12",
      "contact":{
         "phone":"6405106300"
      }
   }
}

Feel free to check out the code here: https://stackblitz.com/edit/angular-ivy-osf83a?

Answer №1

I successfully resolved the issue by updating my Contact typescript file from using NgForm to NgModelGroup.

import { Component, OnInit } from '@angular/core';
import { ControlContainer, NgModelGroup } from '@angular/forms';

@Component({
  selector: 'app-contact',
  templateUrl: './contact.component.html',
  styleUrls: ['./contact.component.css'],
  viewProviders: [{ provide: ControlContainer, useExisting: NgModelGroup }],
})
export class ContactComponent implements OnInit {
  constructor() {}

  ngOnInit() {}
}

Previously, my code looked like this. I only needed to make changes in two places:

import { Component, OnInit } from '@angular/core';
import { ControlContainer, NgForm } from '@angular/forms';

@Component({
  selector: 'app-contact',
  templateUrl: './contact.component.html',
  styleUrls: ['./contact.component.css'],
  viewProviders: [{ provide: ControlContainer, useExisting: NgForm }],
})
export class ContactComponent implements OnInit {
  constructor() {}

  ngOnInit() {}
}

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

Exploring abstract classes for diverse implementation strategies?

Consider the following scenario: export abstract class Button { constructor(public config: IButton) {} abstract click(); } Now, we have a concrete class: class ButtonShowMap extends Button { private isShow = false; constructor(public config: IBu ...

Using VueJs and typescript, transform the input image into Base64 format

Welcome to my first question on this platform! I am looking for a way to convert an input file (image) from a form using VueJs 3 and typescript to Base64 in order to "send" it to my backend (java, spring boot) and store it in mongodb as part of a "User" m ...

When posting on social platforms, the URL fails to display any metadata

We recently completed a project (Web Application) using React .net core with client-side rendering in React. One challenge we encountered was that when the app loads in the browser, it only displays the static HTML initially without the dynamic meta tags. ...

Combining Different Types of Errors

Can TypeScript's type system be exploited to provide additional information from a repository to a service in case of errors? I have a service that needs a port for a repository (Interface that the Repository must implement), but since the service mu ...

Solving Angular Circular Dependencies

My popupservice allows me to easily open popup components: export class PopupService { alert() { this.matdialog.open(PopupAlertComponent); } yesno() { this.matdialog.open(PopupYesNoComponent); } custom() { this.matdialog.open(PopupCustomCompon ...

The Kendo-datepicker always excludes the number zero in the day section

When I try to enter the date: 5/01/2017 The first zero in the days section is always missing when using kendo-date-picker, resulting in the following date: 5/12/17 In my HTML code: <kendo-datepicker [min]="min" [max] ...

What is the best way to incorporate TypeScript variables into CSS files?

In my Angular project, I am aiming to utilize a string defined in Typescript within a CSS file. Specifically, I want to set the background image of a navbar component using a path retrieved from a database service. Although I came across suggestions to use ...

Angular login issue: Error message - Correlation failed. Location not recognized

My application consists of two main elements: An IdentityServer4 host (Asp.NET Core 2.2 application with Asp.NET identity) running on http://localhost:5000 An Angular client app (Angular v7.2.12) operating on http://localhost:5002 The goal is to have the ...

Encountering issues with material 2 autoComplete onSelect() function after transitioning to Angular4

I recently made the transition to Angular 4 and material 2, but I'm encountering an issue with the autoComplete's onSelect() on md-option. It seems to be not working anymore and I can't seem to find any relevant documentation. Has anyone els ...

Angular 4: The process of fetching the URL before the previous one

Upon clicking the /getEmployees URL, the EmployeesComponent will be triggered to show the employee list. If I were to click on a specific employee, say N, the EmployeeComponent would then launch. However, I am interested in finding a way to retrieve the o ...

Unable to create an Ionic module with routing using Angular

Having trouble generating modules with routing in my Ionic project. After creating a new Ionic project using ionic start routing blank, I attempted to generate a module with routing by entering the following commands: ionic g m heroes --route heroes --mo ...

NextJs Link component does not refresh scripts

While using the <Link> tag in NextJs for page navigation, I encountered an issue where my scripts do not rerun after switching pages. The scripts only run on the initial page load or when I manually reload the page. This behavior is different when us ...

Steps for incorporating a filter feature in an Angular Material table

.HTML: Can someone assist me with implementing a filter in this component? I tried using it with the expansion table, but unfortunately, the filter is not working as expected. <mat-dialog-content style="width: 63rem; overflow: initial;"&g ...

Tips on creating type definitions for CSS modules in Parcel?

As someone who is brand new to Parcel, I have a question that may seem naive. In my project, I am using typescript, react, less, and parcel. I am encountering an error with typescript stating 'Cannot find module 'xxx' or its corresponding t ...

When incorporating @tailwind base, the PrimeNG styles fail to take effect

I have been immersed in a project that combines Angular and Tailwind CSS, and we recently made the decision to incorporate PrimeNG for its useful components. However, we encountered an issue where the styles from PrimeNG were not being applied despite be ...

ng-select issue: list not refreshing

I am encountering an issue with the method below that updates the modules array. Even though the console displays the result correctly, the ng-select does not update the list accordingly. I attempted to use this.modules=[...elements], but it did not work ...

Is it possible for Angular2 to map a lone JSON object?

Dealing with a JSON response that is a single object, rather than an array, can be tricky. Recently in my project, I encountered a situation where I needed to map and use such a response from an API to fill out a template. It seemed like a simple task at f ...

The term 'components' has not been defined (no-undef)

Recently integrated Vue into an existing project and encountered a peculiar linting error: error: 'components' is not defined (no-undef) at src/App.vue:13:3: 11 | 12 | @Component({ > 13 | components: { HelloWorld }, | ^ 14 | }) ...

What causes the app to crash in release mode when importing a TypeScript component, while no issues arise in debugging?

Having an issue with importing a bottom sheet written in typescript into a class component. It works correctly in debugging mode but unfortunately not in release mode. Despite checking the logcat, no readable error code or message is being printed. Even a ...

Error TS2322: You cannot assign a Promise<any> to a string type

Having an issue in my react app where I am attempting to import the img source but encountering an error: TS2322: Type 'Promise<any>' is not assignable to type 'string'. What is the correct way to import an element into a variabl ...