How to get formControlName Programmatically in Angular 5 or above

MWN picture MWN · Jun 1, 2018 · Viewed 28.6k times · Source

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

Answer

MWN picture MWN · Jun 1, 2018

from this input field to get formControlName

<input formControlName="item_name" #itemName (input)="inputChanged(itemName)">

You only need to get the attribute formControlName

inputChanged(element: HTMLElement) {
  log(element.getAttribute('formControlName')) // item_name 
}