AngularJS ng-changeDirective

AngularJS 参考手册AngularJS Reference Manual


AngularJS Example

Execute a function when the value of an input field changes:

<body ng-app="myApp">

<div ng-controller="myCtrl">
    <input type="text" ng-change="myFunc()" ng-model="myValue" />
    <p>The input field has changed {{count}} times.</p>
</div>

<script>
angular.module('myApp', [])
.controller('myCtrl', ['$scope', function($scope) {
    $scope.count = 0;
    $scope.myFunc = function() {
        $scope.count++;
    };
}]);
</script>

</body>

Try it Yourself »

Definition and Usage

ng-changeThe ng-change directive tells AngularJS what to do when the value of an HTML element changes.

ng-changeThe ng-change directive needs to be used withng-modelthe ng-model directive.

AngularJS ng-changeThe ng-change directive does not override the native onchange event. If that event is triggered,ng-changeboth the expression and the native onchange event will be executed.

ng-changeThe event is triggered on every change of the value; it does not need to wait for a completed modification process or for the action of losing focus.

ng-changeThe event only targets actual changes to the input field's value, not modifications made via JavaScript.


Syntax

<element ng-change="expression"></element>

<input>, <select>, and <textarea> elements are supported.


Parameter Value

Value Description
expression An expression to execute when the element's value changes.

AngularJS 参考手册AngularJS Reference Manual

Other Extensions