Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

ng-model does not update when sending a value to an input field #12622

Closed
jetta20162 opened this issue Aug 19, 2015 · 3 comments
Closed

ng-model does not update when sending a value to an input field #12622

jetta20162 opened this issue Aug 19, 2015 · 3 comments

Comments

@jetta20162
Copy link

example: i have 3 input fields. The values from the first and second input field get's calculated and the result is shown in the third inputbox.

var1 = parseFloat($("#input1").val();
var2 = parseFloat($("#input2").val();
result = var1 + var2;

$("#input3").val(result);

<input type="number" id="input3" name="input3" ng-model="editForm.input3" ng-currency class="form-control input-sm" placeholder="input3">

i can see the result on the form in input3, but how can i get the model updated?

@bcherny
Copy link
Contributor

bcherny commented Aug 19, 2015

try this-

$("#input3")
  .val(result)
  .trigger('change')

generally, you want to update the model your input is bound to (and let angular propagate it to the view) instead of updating your view directly. the latter is typically only used in unit tests.

@gkalpak
Copy link
Member

gkalpak commented Aug 19, 2015

Actually, this doesn't feel like Angular code. In Angular, it is the other way around: You update the model and the change is reflected in the view. (The model is the single source of truth.)

So, instead of trying to set the value and expect the model to be updated, you should update the model (editForm.input3) and the element's value would be updated as a result.

That said, this is a general support question and should be directed to the appropriate channels.
GitHub issues are reserved for bug reports and feature requests.

@gkalpak gkalpak closed this as completed Aug 19, 2015
@irufus
Copy link

irufus commented Mar 29, 2018

This came handy for my typeahead+bloodhound integration with angularJS

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants