Dnyanesh
Dnyanesh

Reputation: 51

How to get [(ngModel)] name in controller angular 4

I want get [(ngModel)] name in .ts file when input is in active state(when I clicked in input type to write something). I am able to access value of it in controller but I need to access name of it. eg. I have code as follow

<input type="text" id="docno" class="form-control" (click)="focusFunction($event)" [(ngModel)]="docData.docNum" required>

from Above,

I need [(ngModel)] name i.e "docData.docNum" in .ts file.

Please help me to get name of [(ngModel)]. can I get name into event ?

Upvotes: 1

Views: 2096

Answers (2)

Prachi
Prachi

Reputation: 3574

You can add (focus) function for getting the value on click.

    <input type="text" id="docno" class="form-control" (focus)="focusFunction($event)" [(ngModel)]="docData.docNum" required>

And listen it in the .ts file using below code:

    focusFunction(event){
    console.log(event.target.value); 
     }

or by simply writing

    focusFunction(event){
    console.log(this.docData.docNum); 
     }

Upvotes: 0

Rohit.007
Rohit.007

Reputation: 3502

Pass the second parameter in the clicked function as hard coded string with the model name.

Upvotes: 1

Related Questions