ng-change需與ng-model結合使用,官網說明如下:Note, this directive requires ngModel
to be present.
<label for="ng-change-example1">example1:</label> <input type="checkbox" id="ng-change-example1" ng-model="ngChange"/>
<label for="ng-change-example2">example2:</label>
<input type="checkbox" id="ng-change-example2" ng-model="ngChange" ng-change="change()"/><!--ng-change要結合ng-model使用-->
<p>改變次數:{{count}}</p>
change()方法:
$scope.count = 0; $scope.change = function(){ if($scope.ngChange){ alert('被選中'); }else{ alert('未被選中'); } $scope.count++; }
當點擊example2時會觸發change()方法,ngChange變量會發生變化,因此example1也會變化。