如何在angularjs

时间:2015-11-06 19:17:45

标签: javascript jquery angularjs asp.net-mvc checkbox

我正在使用AngularJS和MVC。我有一个带复选框的表,代码如下:

<div class="table table-responsive">
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th>
                            <input type="checkbox" checkbox-all="categories.isSelected" /></th>
                        <th>Category Name</th>
                        <th>Description</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    <tr dir-paginate="category in categories | filter:search_txt | itemsPerPage: pageSize" current-page="currentPage">
                        <td style="padding-left: 9px; padding-top: 1px; padding-bottom: 1px; vertical-align: middle">
                            <div style="height: 35px; overflow: auto;">
                                **<input type="checkbox" ng-model="selected[category.categoryID]" />**
                            </div>
                        </td>
                        <td style="padding: 1px; vertical-align: middle">
                            <div style="height: 35px; overflow: auto;">{{category.categoryName}}</div>
                        </td>
                        <td style="padding: 1px; vertical-align: middle">
                            <div style="height: 35px; overflow: auto;">{{category.description}}</div>
                        </td>
                        <td style="padding: 1px; vertical-align: middle">
                            <div style="height: 35px; overflow: auto;">
                                <a class="btn btn-primary" href="#/category/{{category.categoryID}}" title="Edit">
                                    <i class="glyphicon glyphicon-edit"></i></a>
                            </div>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>

有一个超链接,单击超链接将只选中已检查的值(categoryID)并将它们发送到.js控制器。 按钮控件如:

<a class="btn btn-danger" ng-click="removeSelectedRows()" ng-controller="categoryMultiDeleteController"><i class="glyphicon glyphicon-trash"></i> Delete</a>

控制器代码如下:

myApp.controller('categoryMultiDeleteController',
    ['$scope', 'categoryDataService',
    function ($scope) {
        $scope.removeSelectedRows = function() {
            $scope.categories = $.grep($scope.categories, function (category) {
                return $scope.selected[category.categoryID];
            });
        }
    }]);

我可以访问上面的.js控制器但是无法以数组的形式获取检查值以便删除。

还有另一个.js文件,它类似于“数据服务”,它最终将数组从controller.js文件发送到MVC控制器进行删除。数据服务的代码如下:

var _selectedRows = function (_categories) {
        var deferred = $q.defer();
        var controllerQuery = "category/DeleteMultipleCategory/" + _categories;

        $http.post(controllerQuery)
          .then(function (result) {
              deferred.resolve();
          },
          function (error) {
              // Error
              deferred.reject();
          });
        return deferred.promise;
    };

我的问题是如何从UI到angularJS控制器获取复选框值,并从那里获取数组形式的MVC控制器。

1 个答案:

答案 0 :(得分:0)

如果您不反对从列表中修改类别对象,我通过向每个对象添加一个选定的标记来解决相同类型的问题(带有批量删除复选框的重复列表)列表。如果您将复选框绑定到<input type="checkbox" ng-model="category.selected" />,则在removeSelectedRows上,您只需为已选择== true的类别中的每个类别选择ID。