©
This document uses PHP Chinese website manual Release
当用户改变输入时计算给出的表达式。表达式会被立即计算,不像 JavaScript的onchange事件只会在最后一次改变时触发(通常,当用户离开表单元素或按回车键时)。当值的变化来自于模型时,不会对表达式进行计算。
注意,这个指令需要同时给出 ngModel
。
<input
ng-change="">
...
</input>
参数 | 类型 | 详述 |
---|---|---|
ngChange | expression | 在输入值改变时执行的表达式。 |
<script>
angular.module('changeExample', [])
.controller('ExampleController', ['$scope', Function($scope) {
$scope.counter = 0;
$scope.change = Function() {
$scope.counter++;
};
}]);
</script>
<div ng-controller="ExampleController">
<input Type="checkbox" ng-model="confirmed" ng-change="change()" id="ng-change-example1" />
<input Type="checkbox" ng-model="confirmed" id="ng-change-example2" />
<label for="ng-change-example2">Confirmed</label><br />
<tt>debug = {{confirmed}}</tt><br/>
<tt>counter = {{counter}}</tt><br/>
</div>
protractor.js
var counter = element(by.binding('counter'));
var debug = element(by.binding('confirmed'));
it('should evaluate the expression if changing from view', Function() {
expect(counter.getText()).toContain('0');
element(by.id('ng-change-example1')).click();
expect(counter.getText()).toContain('1');
expect(debug.getText()).toContain('true');});
it('should not evaluate the expression if changing from model', Function() {
element(by.id('ng-change-example2')).click();
expect(counter.getText()).toContain('0');
expect(debug.getText()).toContain('true');});