dongyang2229 2015-03-12 07:08
浏览 97

我应该在angular.js中写下通用控制器函数?

I am writing some functions for check/uncheck all for table list and it is working fine,

Controller is,

    invoiceApp.controller('itemController', ['$scope', 'itemService', '$route', function ($scope, itemService, $route) {
$scope.checkAllItem;
        $scope.listItem = {
            selected: []
        };
        $scope.checkUncheck = function () {
            if ($scope.checkAllItem) {
                $scope.listItem.selected = $scope.items.map(function (item) {
                    return item.id;
                });
            } else {
                $scope.listItem.selected = [];
            }
        };

HTML TABLE,

   <table id="dt_basic" class="table table-bordered table-hover" width="100%">
                    <thead>                         
                        <tr>
                            <th class="text-center" width="5%">
                                <input type="checkbox" name="checkbox-inline" ng-model="checkAllItem" ng-click="checkUncheck()">
                                <input type="checkbox" name="checkbox-inline" ng-click="uncheckAll()">
                            </th>
                            <th width="15%" ng-click="sort()">Name<a href="javascript:void(0)"><i class="fa fa-sort small"></i></a></th>
                            <th width="65%">Description</th>
                            <th width="5%">Unit</th>
                            <th width="10%">Rate</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr ng-repeat="item in items" data-toggle="modal" data-target="#itemModel" ng-click="getItem(item.id)" style="cursor: pointer"> 
                            <td class="text-center">
                                <input type="checkbox" checklist-model="listItem.selected" checklist-value="item.id">
                            </td>
                            <td><a>{{item.name}}</a></td>
                            <td>{{item.description}}</td>
                            <td>{{item.unit}}</td>
                            <td>{{item.rate}}</td>
                        </tr>
                    </tbody>
                </table>

It is working fine,Here my problem is,In my project I have many tables in different pages,I have to copy past this same code (Talking about Controller only ) to everywhere.Is there any method to write it generally?

I tried with $routescope, but It is not working with ng-model,Is there any method to implement the same?

  • 写回答

2条回答

  • doucong1992 2015-03-12 08:27
    关注

    You could turn it into a service then inject the service to whichever controller needs it. You can now also include other commonly used functions used to manipulate data in those. See,

    http://jsbin.com/madapaqoso/1/edit

    app.factory("toolService", function(){
            return {
               checkUncheck: function(listItem) {
                   listItem.selected = [];
               }
            }
    });
    

    I didn't add the added complexity of your function, but you get the idea.

    Alternatively, use a directive. I show it in the jsbin as well. Though, I'd prefer a service since services are made for managing data and directives are more concerned with DOM editing and binding $watchers/events etc. Or perhaps you could persist the data with a service, then use a custom directive to handle all the clicks on the table.

    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧