©
Dieses Dokument verwendetPHP-Handbuch für chinesische WebsitesFreigeben
当用户改变输入时计算给出的表达式。表达式会被立即计算,不像 JavaScript的onchange事件只会在最后一次改变时触发(通常,当用户离开表单元素或按回车键时)。当值的变化来自于模型时,不会对表达式进行计算。
注意,这个指令需要同时给出ngModel
。
ng-change="">...
参数 | 类型 | 详述 |
---|---|---|
ngChange | expression | 在输入值改变时执行的表达式。 |
angular.module('changeExample',[]).controller('ExampleController',['$scope',Function($scope){$scope.counter=0;$scope.change=Function(){$scope.counter++;};}]);ng-controller="ExampleController">Type="checkbox"ng-model="confirmed"ng-change="change()"id="ng-change-example1"/>Type="checkbox"ng-model="confirmed"id="ng-change-example2"/>
/>debug = {{confirmed}}
counter = {{counter}}
protractor.js
varcounter=element(by.binding('counter'));vardebug=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');});