1

I have the html below:

<form #f="ngForm">
  <input type="text" name="dd" value="abc" (change)="foof(f.form.value)"/>
</form>

However, the f.form.value has nothing in it when foof function is called when I change the input text value. Only if I add ngModel to the input element like below,

 <input type="text" name="dd" [ngModel]="abc" (change)="foof(f.form.value)"/>

Then f.form.value has the dd input text value. I don't understand why is this. Do I have to use ngModel to get the form variable to work right?

Note: For certain reasons in our app, we have to submit the whole form when the dd input is changed, so I do need foof(f.form.value). I also don't want two way binding, just one way binding due to variations between the model and the html layout.

2 Answers 2

1

I guess you could make a hack like this:

<form #f="ngForm">
  <input type="text" id="someInput" value="abc" (change)="foof()"/>
</form>

and

x:any;

foof() {
    this.x = document.getElementById("someInput");
    console.log(this.x.value);
}

Don't know your app, so this (terrible) hack might not at all suit your needs.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks!! This solved this problem and simultaneously solved another problem for me where I have to have a form spread across multiple components and was not using reactive forms but template driven forms. Using javascript made that possible too!
Great! Glad I could help! :)
0

I don't think this is how you're supposed to be binding to an input field.

You should be doing it like this,

 <input type="text" name="dd" [(ngModel)]="abc" (ngModelChange)="foof(abc)"/>

1 Comment

For certain reasons in our app, we have to submit the whole form when the dd input is changed, so I do need foof(f.form.value). I also don't want two way binding, just one way binding due to variations between the model and the html layout.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.