简体   繁体   中英

Why function on ng-checked is not getting called

I have called a function on ng-checked and through that function it is getting decided whether checkbox will be checked or not. But the function is not getting called at the load of page.

html -

<label>
     <input type="checkbox" name="status1" id="status1" ng-click="vm.updateStatus('processing')" ng-checked="vm.isSelectedStatus('processing')"> In progress
</label>

JS(controller) -

var vm = this;
vm.isSelectedStatus = isSelectedStatus;

vm.tasksStatus = ["processing", "finished", "failed"];

function isSelectedStatus(status) {
  return vm.tasksStatus.indexOf(status) > -1;
}

function updateStatus(status) {
        var index;

        index = vm.tasksStatus.indexOf(status);

        if(index === -1) {
            vm.tasksStatus.push(status);
        } else {
            vm.tasksStatus.splice(index, 1)   
        }
    }

The code is working properly as I can see..

You almost posted your full code, why not make some minimal extra effort to make it a runnable snippet? Then you can know this part of the code has no issue.

 angular.module('test', []).controller('Test', Test); function Test() { var vm = this; vm.isSelectedStatus = isSelectedStatus; vm.updateStatus = updateStatus; vm.tasksStatus = ["processing", "finished", "failed"]; function isSelectedStatus(status) { return vm.tasksStatus.indexOf(status) > -1; } function updateStatus(status) { var index; index = vm.tasksStatus.indexOf(status); if (index === -1) { vm.tasksStatus.push(status); } else { vm.tasksStatus.splice(index, 1) } } } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script> <div ng-app='test' ng-controller='Test as vm'> <label> <input type="checkbox" name="status1" id="status1" ng-click="vm.updateStatus('processing')" ng-checked="vm.isSelectedStatus('processing')"> In progress </label> <div>{{vm.tasksStatus}}</div> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM