C
C
Camaro672015-04-03 07:42:19
JavaScript
Camaro67, 2015-04-03 07:42:19

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

3 answer(s)
E
Evgeny Voronin, 2015-04-03
@Camaro67

Emulate the 'change' event:

jQuery("#input").val("Новое значение").change() 
либо 
jQuery("#input").val("Новое значение").trigger('change')

jsfiddle.net/7opjcw9c

D
Dmitry Demin, 2015-04-03
@keksmen

Question: why? Isn't it easier to make a change in the model itself?

V
VaaL2004, 2015-04-03
@VaaL2004

$scope.$apply();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question