AngularJS:控制器VS服务 [英] AngularJS: controller vs service

查看:112
本文介绍了AngularJS:控制器VS服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了有关angularjs实体的正确使用一对夫妇的帖子:服务,工厂,控制器和指令

I have read a couple of posts regarding proper usage of angularjs entities: services, factories, controllers and directives.

我特别关注的是控制器和服务的比较。的帖子虽然没有告诉我什么是控制器可以做什么服务不能反之亦然。

My particular concern is a comparison of a controller and a service. None of the posts though told me what is that a controller can do what service cannot and vice versa.

可以这样上市交易或者是它只是一个被角中的使用规范的事情?

Can this be listed or is it just a matter of being canonical in angular's usage?

推荐答案

控制器通常用于以期约束。控制器管理视图的生命周期,并且应该被认为是视图控制器。一个新的控制器将会为视图的每个实例被创建,这意味着如果你从某个视图导航离开,然后再回来 - 或者如果你曾经不止一次某些视图实例一项新的控制器将在每次创建

Controllers are typically used to be bound with a view. Controllers manage a view's life cycle, and should be thought of as View Controllers. A new controller will be created for each instance of a view, meaning that if you navigate away from a certain view, and then back again - or if you have more than once instance of a certain view, a new controller will be created each time.

服务通常用作应用程序的业务逻辑。服务是类似的,因为它们曾经创造意识单身,和实例保持整个应用程序的整个生命周期。这是把你的逻辑功能,许多观点或组件都需要,而且还抱着全局缓存,需要在你的应用程序在整个多个区域访问的好地方。

Services are typically used as the business logic of your application. Services are similar to singletons in the sense that they are created once, and the instance is maintained throughout the entire life cycle of your application. It is a good place to put your logical functions which many views or components will require, and also hold global cache which needs to be accessed throughout multiple areas in your application.

这篇关于AngularJS:控制器VS服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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