?
本文檔使用 PHP中文網(wǎng)手冊(cè) 發(fā)布
當(dāng)用戶改變輸入時(shí)計(jì)算給出的表達(dá)式。表達(dá)式會(huì)被立即計(jì)算,不像 JavaScript的onchange事件只會(huì)在最后一次改變時(shí)觸發(fā)(通常,當(dāng)用戶離開(kāi)表單元素或按回車鍵時(shí))。當(dāng)值的變化來(lái)自于模型時(shí),不會(huì)對(duì)表達(dá)式進(jìn)行計(jì)算。
注意,這個(gè)指令需要同時(shí)給出 ngModel
。
<input
ng-change="">
...
</input>
參數(shù) | 類型 | 詳述 |
---|---|---|
ngChange | expression | 在輸入值改變時(shí)執(zhí)行的表達(dá)式。 |
<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');});