How can I retrieve the value of a nested reactive form in Angular?

I'm working with a nested form setup that looks like this:

profileForm = new FormGroup({
  firstName: new FormControl(''),
  lastName: new FormControl(''),
  address: new FormGroup({
    street: new FormControl(''),
    city: new FormControl(''),
    date1: new FormControl(''),
    date2: new FormControl('')
  })
});

My goal is to make the date2 minimum date dependent on the value of date1, as shown below:

<mat-form-field class="datepickerformfield" floatLabel="never">
    <input matInput class="dp" formControlName="date2" [min]="profileform.controls['date1'].value" [matDatepicker]="date2" placeholder="DD/MM/YYYY" >
</mat-form-field>

I've also attempted:

[min]="profileform.address.controls['date1'].value"

And

[min]="profileform.controls[address].controls['date1'].value"

However, I keep receiving an error message stating:

Cannot read property 'value' of undefined

How can I properly access the value of date1 using the profileform object?

Answer №1

Resolved successfully using:

[min]="profileform.get('address.date1').value"

Currently developing with Angular6+

Answer №2

Within the form, there is a specific element named value that mirrors your form setup but only contains the values. You can retrieve the data by using the code

[min]="profileform.value.address.date1"
.

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

Nativescript encountered an issue while attempting to generate the application. The module failed to load: app/main.js

I'm currently experimenting with the sample-Groceries application, and after installing NativeScript and angular 2 on two different machines, I encountered the same error message when trying to execute: tns run android --emulator While IOS operations ...

ngPrime table column selection and data extraction

I am looking to extract multiple columns from a table. Can anyone suggest the best approach for this? Does NGPrime offer any functionality for achieving this task? Appreciate your help! ...

Managing the action that occurs when the Angular2-toaster pop-up is clicked

Currently, I am in the process of working on a project which incorporates angular2-toaster. // Display message notification this.toasterService.pop('success', `New message from ${data.sender.name} (${data.sender.hid})`, data.message); ...

TSLint is encountering the error code TS2459: The module "@azure/core-tracing" claims to have a local declaration of "Span" but does not export it, along with additional errors

I'm completely lost on how to tackle this error. The message I'm getting doesn't provide much insight, other than indicating an issue with the installation of '@azure/ai-text-analytics'. I've gone through the process of uninst ...

Encountering an issue with Angular2 where it is unable to load a JSON file, presenting the error message: "Cannot resolve all parameters

I've been trying to incorporate a json file into my Angular app, but I can't seem to pinpoint the issue. The error message keeps indicating that it cannot resolve all parameters of my component. (I had no trouble loading data directly from the c ...

Exploring StickIt: Binding the length property from a backbone.Collection

Exploring the use of Backbone, Marionette (1.8.3), StickIt, and TypeScript to effectively bind the length of a Backbone collection in real-time as items are added or removed. As someone new to StickIt, here's my current attempt: export class SomeVie ...

In TypeScript, a generic function with an index constraint

Is it possible to define an element that operates in the manner indicated here? function fn<T, U extends keyof T, T[U] extends number>() I am having trouble making the "T[U] extends number" portion function correctly. ...

Issue with RxDB: Collection not found upon reload

Exploring the integration of RxDB in my Angular project. I wanted to start with a simple example: export const LANG = { version: 0, title: "Language Key", type: "object", properties: { key: { type: "string", primary: true } }, requ ...

Rendering a React/Material UI class based on the state variable in a conditional manner

I am currently working on building a basic navbar setup using React and Material UI. I have encountered an issue where React-Router-Dom does not seem to be functioning within this particular project, and implementing it would be excessive for the simple ta ...

Testing a callback function within a method in Angular

I am currently working with Angular 11 using TypeScript and I am unsure how to properly test scenarios where an exception is raised within my method. myMethod() { this.myService.callBackend(id).subscribe(() => { // do something when success ...

Tips for deploying an Angular Universal 9 application on a live server

Our Angular 9 app functions perfectly when deployed on an IIS server. We also have a version of the app that has been integrated with Universal by another company. Now, we need to figure out how to deploy our app with server-side rendering into productio ...

Function type guards in Typescript do not support type inference

When checking for null in alpha, I validate the result and use throw new Error if needed. However, even after doing so, the compiler still indicates a compilation error: const obj = { objMethod: function (): string | null { return 'always a str ...

When working with Typescript and React, you may encounter an issue where an element implicitly has an 'any' type because the type 'State' has no index signature. This can lead to

In my current mini project, I am using Typescript and React. As someone new to Typescript, I am currently in the process of learning it. Within the project, I have a state defined as follows: type State = { field1: string, field2: string, field3: n ...

Receiving undefined when subscribing data to an observable in Angular

Currently, I am facing an issue in my Angular project where subscribing the data to an observable is returning undefined. I have a service method in place that retrieves data from an HTTP request. public fetchData(): Observable<Data[]> { const url = ...

Combine the names of classes in an array into a union type

Can someone help me with extracting all the methods from an Array of Classes into one type Union? Here's an example: class A{ getBooks(): Book[]{} getBook(): Book{} } class B{ getUsers(): User[]{} getUser(): User{} getBooksOfUser(userId: s ...

Error message: Unable to locate provider for @Attribute('sampleString')

We are currently working on writing unit tests for a component that utilizes a third-party JavaScript library. The constructor of our component is structured as follows: @Constructor(@Inject(ElementRef) private eleref: ElementRef, @Attribute('sampleS ...

The Angular 6 component adapts to different routes, but encountered an issue: the selector "app-navbar" could not find a matching element

My angular application consists of two separate parts: One for regular users, which is the current view One dedicated to administrators featuring an admin dashboard I am aiming to incorporate these two different views into my app.component.html file. a ...

Utilize style as a module in Angular

The issue at hand: Let's take a look at this project structure: /src /public /styles /general /tables.scss /secure /components /someTable1 /someTable.component.ts /someTable.component.css /someTa ...

Determine the number of lines in a textarea, taking into account both manual

I have a textarea that I need to validate for input exceeding 6 rows. My JavaScript code works perfectly by checking for scrollbar presence, indicating more than 6 lines of input. However, how can I achieve the same validation using PHP in case JavaScript ...

Identifying patterns within a string using JavaScript and TypeScript

Given a user-input pattern, for example [h][a-z][gho], and a string "gkfhxhk", I am attempting to determine if the string contains the specified pattern. The pattern dictates that the first character must be 'h', followed by any letter from a-z, ...