0

すでに関数がバインドされている削除ボタンを切り替えようとしています。リストは、ユーザー オブジェクトをチェックする ng-repeat で作成されます。しかし、これまで見てきた方法は単純なモデルショーであり、私の場合は機能しません。または複雑なディレクティブ コントローラー メソッド。

必要なのは、少なくとも 1 つのチェックボックスがオンになっているかどうかを確認することだけです。コードのスニペットは次のとおりです。

                    <table class="table">
                    <tr ng-repeat="user in ctrl.commonUserService.users | filter:ctrl.commonUserService.suppressLoggedOnUserFilter()">

                        <td><input type="checkbox" ng-model="user.selected" value="y" /></td>
                        <td title="User Name"><strong>{{user.userName}}</strong></td>

Angularでこれを簡単に行うにはどうすればよいですか? ユーザーをループして、ユーザーが選択されているかどうかを確認する関数を作成しようとしましたが、この関数を ng-show に配置できるとは思いません。

4

2 に答える 2

0

ng-change入力にディレクティブを使用すると、これをかなり簡単に行うことができます。実際の例については、このplunkrを参照してください。ただし、基本的には次のようになります。

意見:

<table class="table">
    <tr ng-repeat="user in users">
    <td><input type="checkbox" ng-change="isSelected()" ng-model="user.selected" /></td>
    <td title="User Name"><strong>{{user.userName}}</strong></td>
    </tr>
</table>
<button class="delete" ng-show="showDelete">Delete</button>

コントローラー

$scope.isSelected = function() {
    var somethingSelected = false;
    $scope.users.forEach(function(user, index) {
        if (user.selected) somethingSelected = true;
    });
    $scope.showDelete = somethingSelected;
};
于 2015-09-01T16:03:16.957 に答える