Need to know, when you have a multiple controls in a form and you want to know to which control user changed so that you can take some actions.
<input formControlName="item_name" #itemName (input)="inputChanged(itemName)">
Why I need to get formControlName?
as you can see in the image, some fields are edited but not confirmed that's why user sees options to verify or cancel the operation on that specific field. That's why I require to get formControlName
of input-changed field so that I can display the options only to that field.
I have searched for its solution but couldn't found on stack-overflow
that's why I decided to post this question with answer
To fetch the value of a form control, we have to use value property on the instance of FormControl in our class. In the same way we can fetch the value in HTML template. city = new FormControl('Noida'); console.
This means that: if we are using template driven forms, we will be able to plug the custom component to the form simply by using ngModel. and in the case of reactive forms, we will be able to add the custom component to the form using formControlName (or formControl )
Without a parent FormGroup, [formControl]="name" worked earlier because that directive can stand alone, that is, it works without being in a FormGroup. With a parent FormGroup, the name input needs the syntax formControlName=name in order to be associated with the correct FormControl in the class.
Angular forms FormControlName Directive Last Updated : 03 Jun, 2021 In this article, we are going to see what is FormControlName in Angular 10 and how to use it. FormControlName is used to sync a FormControl in an existing FormGroup to a form control element by name.
Need to know, when you have a multiple controls in a form and you want to know to which control user changed so that you can take some actions. Why I need to get formControlName? as you can see in the image, some fields are edited but not confirmed that's why user sees options to verify or cancel the operation on that specific field.
Support for using the ngModel input property and ngModelChange event with reactive form directives has been deprecated in Angular v6 and is scheduled for removal in a future version of Angular. For details, see Deprecated features. Licensed under the Creative Commons Attribution License 4.0.
FormControlName is used to sync a FormControl in an existing FormGroup to a form control element by name. In app.component.ts make an object that contain value for the input. In app.component.html use FormControlName to get values. Serve the angular app using ng serve to see the output.
You can use that approach:
<input formControlName="item_name" #itemName (change)="inputChanged($event)">
When the input's value changes, the change event occurs, and Angular provides a corresponding DOM event object in the $event variable which this code passes as a parameter to the component's inputChanged() method.
inputChanged (event: any) { // without type info
this.myValue = event.target.value;
}
}
Reference link: https://angular.io/guide/user-input
<form [formGroup]="usersForm">
<input type="text" formControlName="name" placeholder="name"/>
</form>
export class AppComponent implements OnInit, OnDestroy {
usersForm: FormGroup;
message: string;
private subscriptions$ = new Subscription();
constructor( private formBuilder: FormBuilder) {}
ngOnInit(): void {
this.usersForm = this.formBuilder.group({
name: '',
age: '',
gender: '',
});
this.subscriptions$.add(this.name.valueChanges.subscribe(value => {
// Do someting
}));
}
ngOnDestroy(): void {
this.subscriptions$.unsubscribe();
}
get name(): AbstractControl {
return this.usersForm.get('name');
}
}
See the complete example in the Stackbliz:
https://stackblitz.com/edit/form-builder-example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With