0

我正在开发一个 AngularJS 应用程序,我是 Angular 的新手,我正在设置一个大对象,以便在需要时$rootScope随时可用$scope

此时它看起来像这样:

Application.run(['$rootScope', function($rootScope) {

    $rootScope.Envato = {

        API : {

            Path : {
                Private : "/api/private/",
                Public : "/api/public/"
            },

            Methods : {

                Private : {

                    User : {

                        Vitals : $rootScope.Envato.API.Path.Private + "get-user-vitals.php",
                        Transactions : $rootScope.Envato.API.Path.Private + "get-earnings-and-sales-by-month.php",
                        Statement : $rootScope.Envato.API.Path.Private + "get-statement.php",
                        Sales : $rootScope.Envato.API.Path.Private + "get-recent-sales.php",
                        Account : $rootScope.Envato.API.Path.Private + "get-account-information.php"
                    },

                    Item : {

                        Purchase : {
                            Information : $rootScope.Envato.API.Path.Private + "get-purchase-information.php",
                            Download : $rootScope.Envato.API.Path.Private + "get-item-download-url.php"
                        }
                    }
                },

                Public : {

                    API : {

                        Releases : $rootScope.Envato.API.Path.Public + "get-api-releases.php"
                    },

                    Blog : {

                        Posts : $rootScope.Envato.API.Path.Public + "get-blog-posts.php",
                        Threads : {

                            Active : $rootScope.Envato.API.Path.Public + "get-active-threads.php",
                            Status : $rootScope.Envato.API.Path.Public + "get-thread-status.php"
                        },
                    },

                    Collections : $rootScope.Envato.API.Path.Public + "get-collections.php",

                    Items : {

                        Count : $rootScope.Envato.API.Path.Public + "get-number-of-items.php",
                        Popular : $rootScope.Envato.API.Path.Public + "get-popular-items.php",
                        Featured : $rootScope.Envato.API.Path.Public + "get-featured-items.php",

                        New : {

                            Marketplace : $rootScope.Envato.API.Path.Public + "get-new-items-from-market.php",
                            User : $rootScope.Envato.API.Path.Public + "get-new-items-from-user.php",
                            Random : $rootScope.Envato.API.Path.Public + "get-random-new-items.php"
                        },

                        Information : $rootScope.Envato.API.Path.Public + "get-item-information.php",
                        Prices : $rootScope.Envato.API.Path.Public + "get-item-prices.php"
                    },

                    Users : {

                        Total : $rootScope.Envato.API.Path.Public + "get-total-users-count.php",
                        Information : $rootScope.Envato.API.Path.Public + "get-user-information.php",
                        Items : {
                            Count : $rootScope.Envato.API.Path.Public + "get-user-items-by-site.php"
                        }
                    },

                    Search : $rootScope.Envato.API.Path.Public + "get-search-results.php"
                }
            }
        }
    };

    $rootScope.$safeApply = function($scope, fn) {

        fn = fn || function() {};

        if($scope.$$phase) {
            fn();
        } else {
            $scope.$apply(fn); 
        };
    };

}]);

但这是一团糟,我想知道是否有更好的方法,或者 AngularJS 有一些其他很棒的东西可以帮助我做类似的事情:)

编辑:显然,类似的东西$rootScope.Envato.API.Path.Private不起作用,但我添加了它,因为它更容易理解我想要做什么。

4

1 回答 1

1

看起来您正在寻找服务。在 AngularJS 网站上查看本指南以了解如何创建它们。

Application.factory('Envato', function() {
  return {
    API: {
      Path: { ... },
    }
  };
});

Application.controller('SomeController', function($scope, Envato) {
  $scope.thing = Envato.API...;
});
于 2013-02-11T17:37:49.187 回答