选中复选框后,不会触发ng-change

时间:2015-08-06 06:46:01

标签: angularjs checkbox

以下是背景信息:" isChecked == 1"然而,虽然它的初始化,它并没有触发" ng-change"第一次"点击"动作。

<td ng-repeat="isCheck in item.check track by $index">
    <input type="checkbox" ng-model="isCheck" ng-checked="isCheck == 1" ng-true-value="1" ng-false-value="0" ng-change="f(isCheck, item, vm.modules[$index])"/>
</td>

2 个答案:

答案 0 :(得分:5)

检查此工作代码:

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function ($scope) {
    $scope.item = {};
    $scope.item.check = [1, 0, 1, 0];

    $scope.f = function(isCheck) {
        console.log(isCheck);
    };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp">
  <div ng-controller="MyCtrl">
    <table>
      <tr>
        <td ng-repeat="isCheck in item.check track by $index">
          <input type="checkbox" ng-model="isCheck" ng-checked="isCheck === 1" ng-true-value="1" ng-false-value="0" ng-change="f(isCheck)"/>
        </td>
      </tr>
    </table>
  </div>
</div>

答案 1 :(得分:0)

如果以编程方式更改ng-model,则

ng-checked不能与ng-change一起使用,请查看官方文档:

https://docs.angularjs.org/api/ng/directive/ngChange