SoFunction
Updated on 2025-04-13

angularjs implements monitoring of form input changes (ng-change and watch)

angularjs implements monitoring of form input changes through ng-change and watch methods

Directly practice code

<!DOCTYPE html>
<html xmlns="http:///1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title></title>
</head>
<body ng-app="myApp" ng-controller="myContro">
  <div>
    <h1>ng-changeinstruction</h1>
    ng-changeinstruction,When form input changes,This event will be triggered<br />
    <div>
      Name:<input type="text"  ng-model=""
        placeholder="Please enter your name" ng-change="inputChange()" />
    </div>
    <div>
      age:<input type="number" ng-model=""
        placeholder="Please enter age" ng-change="inputChange()" />
    </div>
    <div>{{}}</div>
  </div>
  <div>
    <h1>By listening to change andng-changSame effect</h1>
    <div>
      Name:<input type="text"  ng-model=""
        placeholder="Please enter your name" />
    </div>
    <div>
      age:<input type="number" ng-model=""
        placeholder="Please enter age" />
    </div>
    <div>{{}}</div>
  </div>
</body>
</html>
<script src="../JS/"></script>
<script type="text/javascript">
  var app = ("myApp", []);
  ("myContro", function ($scope, $interpolate) {
    $ = {
      name: "",
      age: "",
      message: ""
    };
    $scope.user2 = {
      name: "",
      age: "",
      message: ""
    };
    $ = "{{name}},Hello,You're this year{{age}}It's old!";
    var template = $interpolate($);
    $ = function () {
      $ = template({ name: $, age: $ });
    };
    //// The following is to achieve the same effect as ng-change through watch monitoring    $scope.$watch("", function (newValue, oldValue) {
      $(newValue, oldValue);
    });
    $scope.$watch("", function (newValue, oldValue) {
      $(newValue, oldValue);
    });
    $ = function (value1, value2) {
      if (value1 != value2) {
        $scope. = template({ name: $scope., age: $scope. });
      }
    }
  });
</script>

Summarize

The above is the angularjs implementation of monitoring form input changes introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!