仅当我没有模型

时间:2016-02-09 15:22:57

标签: angularjs angularjs-ng-click

我在我的档案中有这段代码

 <div class="table-responsive">
            <table class="table">
                <thead>
                    <tr>
                        <th><a ng-click="sort('PartnerName')">@TID.Partner</a></th>
                        <th><a ng-click="sort('StartTID')">@TID.StartTID</a></th>
                        <th><a ng-click="sort('EndTID')">@TID.EndTID</a></th>
                        <th><a ng-click="sort('DateBlocked')">@TID.DateBlocked</a></th>
                        <th><a ng-click="sort('Blocked')">@TID.Blocked</a></th>
                    </tr>
                </thead>
                <tbody>
                    <tr ng-repeat="item in items | orderBy:sortOrder:reverseSort | filter:refine | filter: { 'Blocked': showInactive!==true } as filtered ">
                        <td>{{ item.PartnerName }}</td>
                        <td>{{ item.StartTID }}</td>
                        <td>{{ item.EndTID }}</td>
                        <td>{{ item.DateBlocked }}</td>
                        <td> <input type="checkbox" ng-click="UnBlockOne(item);" ng-model="item.Blocked"/></td>
                    </tr>
                    <tr ng-if="processed == false" ng-cloak>
                        <td colspan="5">@TID.Loading</td>
                    </tr>
                    <tr ng-if="filtered.length == 0 && processed == true" ng-cloak>
                        <td colspan="5">@TID.NoResultsFound</td>
                    </tr>
                </tbody>
            </table>
        </div>

我有这个复选框

<td> <input type="checkbox" ng-click="UnBlockOne(item);" ng-model="item.Blocked"/></td>

在我的控制器中调用此方法

   $scope.UnBlockOne = function (item) {
        db.UnBlockOne(item, function () {
            $scope.GetFilteredList(filter);
            $rootScope.processed = true;
        });
    };

奇怪的是,如果我有一个ng-model,则ng-click不起作用。

所以,这是有效的

<td> <input type="checkbox" ng-click="UnBlockOne(item);" ng-model=""/></td>

这不起作用

<td> <input type="checkbox" ng-click="UnBlockOne(item);" ng-model="item.Blocked"/></td>

我在其他文件中有这样的代码,它工作正常。

3 个答案:

答案 0 :(得分:0)

尝试这样,我不确定,因为我无法测试它:

<td> <input type="checkbox" ng-click="UnBlockOne('item');" ng-model="item.Blocked"/></td>

 $scope.UnBlockOne = function (item) {
        db.UnBlockOne($scope['item'], function () {
            $scope.GetFilteredList(filter);
            $rootScope.processed = true;
        });
    };

但是对于复选框,您应该使用ng-change。

答案 1 :(得分:0)

您可能希望查看ng-change而不是ng-click

答案 2 :(得分:0)

而不是ng-click使用ng-change。

参考:https://docs.angularjs.org/api/ng/input/input%5Bcheckbox%5D