为什么要使用AngularJS工厂? [英] Why should I use factories in AngularJS?

查看:225
本文介绍了为什么要使用AngularJS工厂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个应用程序,它不OAuth认证的几个供应商(例如推特,脸谱)。我是新来的角,在UI /型号写在角没有问题。
但是,我已经得到了很多的辅助方法,例如callApi,checkVerification,为gettoken,等等等等。
我想约8的方法。我一直在寻找到在AngularJS建立工厂,但我不知道我为什么要这么做?这将需要大量的改写,我看不出有什么优势?为什么不带辅助方法保持它的简单?


解决方案

一个工厂创建角服务只是其中一种方法。您可以将您的辅助方法,为这些服务。

作为一个角度程序员,未使用的服务是完全一样是一个电工,而不是使用接地线,它不是真正需要的东西,节省时间,不必把它挂,但你正在服用引起火灾的危险如果一些设备有一个很短。在编程的风险中的code灵活性,可维护性和测试的能力方面。如果你的模块中按预期不工作,这将是难以调试等。

当你有code的显著量,辅助函数的逻辑分组到服务,帮助您更轻松地组织或者找到它们并对其进行测试。

这最好的做法是由许多资深的开发谁大angularjs项目工作,以这种方式产生了通过结构code成功的经验建议。它提供了另外一层为您可能会发现以后要插入code。如果您还没有到位,你不会使用它(你刚才说的原因,它会带你的时间重写),然后你的code会刚刚结束了混乱和混乱一段时间。棱角分明的一大特色是鼓励code组织和测试能力。

当然,你可以写code你想要的作品的任何方式,但使用的服务(通过工厂或其他方式)将帮助你的大项目,成为一个非常困难的在长期保持混乱结束术语。像其他的设计模式,很聪明的程序员们发现编码结构和强大产生长期的可维护性利益的最佳做法。

请参阅:为什么角

更多这方面的意见。

I'm writing an app, which does OAuth authentication to several providers (e.g. twitter, facebook). I'm new to Angular, the UI / model is written in Angular without an issue. But, I've got a lot of helper methods, e.g. callApi, checkVerification, getToken, etc. etc. I think about 8 methods. I've been looking into creating factories in AngularJS, but I don't know why I should do this? It will require a lot of rewriting and I don't see the advantage? Why not keep it simple with helper methods?

解决方案

A factory is just one way of creating an angular service. You can group your helper methods into these services.

Being a angular programmer and not using services is exactly like being a electrician and not using the ground wire, its not really needed for anything and saves time to not have to hook it up, but you are taking risks of starting a fire if some appliance has a short. In programming the risks are in terms of code flexibility, maintainability, and test-ability. If one of your modules does not work as intended, it will be harder to debug, etc.

When you have a significant amount of code, logical groupings of helper functions into services helps you more easily organize or find them and test them.

This best practice is suggested by many senior developers who worked on large angularjs projects and have had successful experiences by structuring code in this way. It provides another layer into which you may find you want to insert code later. If you don't have it in place, you won't use it (for the reason you just said, it will take you time to rewrite) and then your code will just end up messier and messier over time. A major feature of angular is to encourage code organization and test-ability.

Of course, you can write code any way you want that works, but using services (via factory or other methods) will help you not end up with large project that becomes a very difficult to maintain mess over the long term. Like other design patterns, very smart programmers have discovered coding structures and best practices that yield powerful long term maintainability benefits.

See: Why use services in Angular?

for more views on this.

这篇关于为什么要使用AngularJS工厂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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