我的ng表正在像这样构建

                    <table ng-table="storeCommandsTableParams" class="table tile">
                    <tr ng-repeat="storeCommand in $data">
                        <td>
                                <input type="checkbox" ng-change="vm.toggleCommandSelection(storeCommand)"  ng-model="vm.selectedCommands" >
                        </td>
                    </tr>
                </table>


而且我有这样的控制器设置。

    var vm = this;
    vm.selectedCommands = { };
    vm.toggleCommandSelection = function (storeCommand) {
        var idx = vm.selectedCommands.indexOf(storeCommand);
        // is currently selected
        if (idx > -1) {
            vm.selectedCommands.splice(idx, 1);
        }
            // is newly selected
        else {
            vm.selectedCommands.push(storeCommand);
        }
    };


我要完成的工作是,当用户单击复选框时,应将相应的storeCommand发送到vm.toggleCommandSelection函数,以便可以选择storeCommands列表。但是不会触发此功能。

Plunker

最佳答案

您的模型不是storeCommand,而是vm.selectedCommands。因此,当您单击复选框时,在这种情况下不会触发ng-change。可能您可以改用ng-click

尝试这个

<input type="checkbox" ng-click="vm.toggleCommandSelection($event, storeCommand)">

    var vm = this;
    vm.selectedCommands = [];
    vm.toggleCommandSelection = function ($event, storeCommand) {
        var idx = vm.selectedCommands.indexOf(storeCommand);
        if ($event.target.checked) {
            if (idx === -1) {
               vm.selectedCommands.push(storeCommand);
            }
        }
        else {
            if (idx > -1) {
              vm.selectedCommands.splice(idx, 1);
            }
        }
    };


演示版
http://plnkr.co/edit/kcW7YCWsXE8mtZoxjm1J?p=preview

09-11 08:22