0

If the view (e.g the value in a text input) is changed by an external javascript, it will not be reflected in the model.

How can I trigger a model update (to read back all binding values from the view).

P.S: for some reasons, manually setting the model value is not an option for me, I just need to call the same function that is called when user is typing in the text box.

2
  • Do you have any control over the external script that changes the value? Commented Mar 30, 2014 at 9:30
  • Actually, yes, but as the project will gradually expand, I don't like to impose artificial constraints/guidlines for javascripts: just plug in whatever suits you and we'll all be happy. Commented Mar 30, 2014 at 9:40

1 Answer 1

2

If you are changing the value of an element, for example input, that uses ng-model it should suffice to trigger the element's 'change' event.

jQuery:

$('#input').val('new value').trigger('change');

No jQuery:

var input = document.querySelector('#input');
input.value = 'new value';

var event = document.createEvent('HTMLEvents');
event.initEvent('change', true, false);
input.dispatchEvent(event);

Demo: http://plnkr.co/edit/IFb8OmegaGAAniy9ttn5?p=preview

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

Comments

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.