angularjs工厂的最佳实践 [英] angularjs factory best practice

查看:79
本文介绍了angularjs工厂的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题将是一个有点争议,但我觉得这实际上可能有助于开发人员希望做的大项目angularJS。

I know this question is going to be a bit debatable but I find this could actually be helpful to developers looking to do big projects with angularJS.

最佳实践(根据约翰爸爸至少)说,这是很好的封装angularjs控制器以一致的功能,并在其中注入提供商/服务/工厂。所以基本上你最终会得到这样的:

Best practices (according to John Papa at least) say that it's good to encapsulate angularjs controllers in an unanimous function and inject the providers/services/factories in them. So basically you would end up with something like this:

(function() {
    "use strict";

    angular
        .module("app.components")
        .controller("ctrl", ctrl);

    ctrl.$inject = ["$scope", "someFactory", "someOtherFactory"];

    function ctrl($scope, someFactory, someOtherFactory) {
        // controller logic
    }
});

如果你在一个大项目的工作最合理的假设是,你会希望有尽可能少冗余,所以我结束了其为每一类API端点与服务器的工厂,所以我让他们通过服务器范围分组

If you work on a big project the most reasonable assumption is that you will want to have as less redundancy as possible so I ended up with having factories for each class of api endpoints from the server so I have them grouped by server scope.

在复杂的屏幕,我会用那些也许五六年,所以你可以想像的注入开始变长。

In a complex screen, I will use maybe five to six of those and so you can imagine the inject starts to get long.

我想要做的是有一个#DATA我可以注入,并通过#DATA我想访问包含$ HTTP调用工厂。

What I would want to do is have a "#data" that I can inject and through that #data I want to access the factories containing the $http calls.

我的感觉是,这#DATA不应该包含其重定向到,但更像是一个提供者,你可以用配置方法注册工厂的所有配置文件中的工厂成才是这样的:

My feeling is that this #data should not be a file containing all the configurations for the factories it redirects to but more like a provider where you can register with a configuration method the factory someting like this:

(function() {
    "use strict";

    angular
        .module("app.factory")
        .factory("someFactory", someFactory);

    someFactory.$inject = ["$http"];

    function someFactory($http) {
        // factory logic
    }

    angular
        .module("app")
        .config(someFactoryConfig);

    someFactoryConfig.$inject = ["#dataProvider"];

    function someFactoryConfig(#dataProvider) {
        #dataProvider.register("someFactory");
    }
});

这将类似于你如何定义UI的路由器内部的状态。

This would be similar to how you define a state inside the ui-router.

这是如何实现供应商的任何想法?

Any ideas on how to implement the provider?

推荐答案

我知道这可能不是回答你的问题,但角度2,尝试从控制器走开 $范围

I know this might not answer your question but Angular 2 tries to go away from Controllers and $scope.

相反,当你开始你的项目,你应该计划将它的功能和从这些功能中创建不同的模块。这些模块中,你将有你的指令(组件),服务和放大器;工厂以支持整个模块。

Instead when you start your project you should plan it into features and from these features create different modules. Within these modules you will have all of your directives (components), services & factories to support the whole module.

这样你封装你的应用程序成更小的应用程序。这将确保您的应用程序的第1部分不工作您的应用程序的其余部分将不会受到影响。

This way you encapsulate your application into smaller applications. This will ensure that if 1 part of your application does not work the rest of your application will not be affected.

角可以是一个难啃的骨头,并得到了很多火焰了点。但是,如果你计划你的项目正确的角度可以是最强大的框架存在之一。

Angular can be a hard nut to crack and gets a lot of flame for that. But if you plan your project correctly Angular can be one of the most powerful frameworks out there.

这篇关于angularjs工厂的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆