在ng-checked中调用函数

时间:2015-05-04 06:23:29

标签: javascript angularjs

我正在尝试在复选框中执行ng-checked上的函数。基本上我想要选中复选框取决于某些条件。我关注   AngularJs ng-checked using a function ..它工作正常。但我发现该函数正在调用6次!!!

html代码:

          <label>
          <input type="checkbox" 
               ng-checked="getCheckedValue()">
        </label>

角度js功能:

            $scope.getCheckedValue = function(){
             alert("checking ...");
             var isChecked=false;
             if(some condition){
              isChecked=true;
               return;
             }
             return isChecked;
            }

我在我的js上得到警报6次!!。请帮帮我

1 个答案:

答案 0 :(得分:0)

根据评论,我认为您可以在加载医生列表后不使用手表更新已检查状态

<table>
    <tr ng-repeat="doctor in doctorList">
        <td data-ng-bind="doctor.firstName"></td>
        <td ng-repeat="timeSlot in timeSlots">
            <input type="checkbox" ng-checked="doctor[timeSlot]" />
        </td>
    </tr>
</table>

然后加载医生名单

    angular.forEach($scope.doctorList, function (doctor) {
            angular.forEach($scope.timeSlots, function (slot) {
            doctor[slot] = $scope.isChecked(doctor, slot)
            });
    });

演示:Fiddle