Answer the question
In order to leave comments, you need to log in
How to let angularjs know that input value has changed?
Hello. There was such a problem with angularjs: if you change the input value programmatically, i.e. via javascript (e.g. document.getElementById("#input").value = "New Value" or jQuery("#input").val("New Value") ), then angularjs does not see these changes. How can this be fixed?
Below is an example of the problematic code. Thank you in advance.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="/angular.min.js"></script>
<script type="text/javascript" src="/jquery-2.1.3.min.js"></script>
<script type="text/javascript">
jQuery(function () {
jQuery("#input").val("Новое значение");
});
</script>
<title>AngularJS</title>
</head>
<body ng-app>
<input id="input" type="text" ng-model="value"><br>
Вы ввели в поле выше: {{value}}<br>
</body>
</html>
Answer the question
In order to leave comments, you need to log in
Emulate the 'change' event:
jQuery("#input").val("Новое значение").change()
либо
jQuery("#input").val("Новое значение").trigger('change')
Question: why? Isn't it easier to make a change in the model itself?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question