结合控制器中的常用功能 AngularJS

Combining common function in controllers AngularJS

我正在处理这个项目。我正在使用分页、checkAll、deleteAll 等所有控制器之间通用的功能。我希望将代码移至工厂或服务,以便可以用于所有控制器。我试过但不清楚是否完美。我要提示。 我知道创建工厂或服务,以及如何将它集成到控制器中,但有点混淆了移动类似代码的逻辑。

我给出了我用来在所有控制器中编写的示例,以及用于排序和分页的逻辑。

$scope.bigCurrentPage = 1;
$scope.editIndex = "";

var sortKey = "name";
var sortDirection = "ASC";
$scope.filterObj = {order: [sortKey + " " + sortDirection], limit: CONFIG.limit, offset: 0};
$scope.filterObj.offset = ($scope.bigCurrentPage - 1) * CONFIG.limit;

$scope.sortList = function (inputSort) {
    if (sortKey === inputSort) {
        sortDirection = (sortDirection === "ASC") ? "DESC" : "ASC";
    } else {
        sortKey = inputSort;
    }
    $scope.filterObj.order = [sortKey + " " + sortDirection];
    $scope.pageChanged();
}


$scope.getSongs = function () {
    $scope.filterObj.offset = ($scope.bigCurrentPage - 1) * CONFIG.limit;
    Song.find({filter: $scope.filterObj})
            .$promise
            .then(function (data) {
                $scope.items = data;
                $scope.maxSize = 5;
                $scope.size = $scope.filterObj.limit;
                $scope.countingIndex = ($scope.bigCurrentPage - 1) * $scope.filterObj.limit; //Its idea to implement counting. Variable is not part of pagination..////     
            });
};


$scope.pageChanged = function () {
    $scope.getSongs();
};

$scope.pageChanged();

这样,假设我正在为 Albums 执行此操作并且我会将 sortKey 更改为 'id',将使用 'Album.find' 的服务,将更改 'filter' 对象并且我完成了,但事实是我在所有控制器中使用通用代码。怎么写比较好

你确实有很多属性很难在服务中维护,所以我建议你应该使用控制器而不是服务,(或者将可重用的变量放在服务中,这会使代码更有意义)。下面是创建的控制器,可以使用 $controller 依赖项在任何其他控制器中注入。

通用控制器

app.controller('commonCtrl', function($scope) {
    $scope.bigCurrentPage = 1;
    $scope.editIndex = "";

    var sortKey = "name";
    var sortDirection = "ASC";
    $scope.filterObj = {
        order: [sortKey + " " + sortDirection],
        limit: CONFIG.limit,
        offset: 0
    };
    $scope.filterObj.offset = ($scope.bigCurrentPage - 1) * CONFIG.limit;

    $scope.sortList = function(inputSort) {
        if (sortKey === inputSort) {
            sortDirection = (sortDirection === "ASC") ? "DESC" : "ASC";
        } else {
            sortKey = inputSort;
        }
        $scope.filterObj.order = [sortKey + " " + sortDirection];
        $scope.pageChanged();
    }

    $scope.getSongs = function() {
        $scope.filterObj.offset = ($scope.bigCurrentPage - 1) * CONFIG.limit;
        Song.find({
                filter: $scope.filterObj
            })
            .$promise
            .then(function(data) {
                $scope.items = data;
                $scope.maxSize = 5;
                $scope.size = $scope.filterObj.limit;
                $scope.countingIndex = ($scope.bigCurrentPage - 1) * $scope.filterObj.limit; //Its idea to implement counting. Variable is not part of pagination..////     
            });
    };

    $scope.pageChanged = function() {
        $scope.getSongs();
    };

    $scope.pageChanged();
});

您可以使用 $controller 依赖项将此创建的 commonCtrl 注入到任何其他控制器,commonCtrl 将其范围共享到注入它的控制器。但是在 commonCtrl 中从 controllerA 所做的更改在 controllerB 中将不可用,因为 commonCtrl 将其范围的副本提供给注入它的控制器。

控制器A

app.controller('controllerA', function($scope, $controller){
   $controller('commonCtrl', {$scope: $scope}); //inject commonCtrl scope to current scope
   //common scope will be available from here
}) 

注意

If you want changes to be reflected in other controller you could use service with this controller which would share common properties to different controllers.