Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get formControlName Programmatically in Angular 5 or above

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.

enter image description here

I have searched for its solution but couldn't found on stack-overflow that's why I decided to post this question with answer

like image 698
MWN Avatar asked Jun 01 '18 00:06

MWN


People also ask

How do I get the FormControl value in Angular 6?

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.

Can we use formControlName with ngModel?

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 )

Can we use formControlName without FormGroup?

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.

What is formcontrolname in angular 10?

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.

Why I need to get formcontrolname?

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.

What happened to ngmodel support for reactive form directives in angular?

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.

How to sync a formcontrol in an existing formgroup?

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.


1 Answers

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

Another alternative more elegant:

template

<form [formGroup]="usersForm">
  <input type="text" formControlName="name" placeholder="name"/>
</form>

component class

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

like image 147
Ricardo Ferreira Avatar answered Oct 12 '22 17:10

Ricardo Ferreira