Separation of concern is at the heart while designing an AngularJS application. Your controller must be responsible for binding model data to views using $scope. It does not contain logic to fetch the data or manipulating it.

For that we must create singleton objects called services. AngularJS can manage these service objects. Wherever we want to use the service, we just have to specify its name and AngularJS auto-magically inject these objects (more on this later).

Thus service is a stateless object that contains some useful functions. These functions can be called from anywhere; Controllers, Directive, Filters etc. Thus we can divide our application in logical units. The business logic or logic to call HTTP url to fetch data from server can be put within a service object.

Putting business and other logic within services has many advantages. First it fulfills the principle of separation of concern or segregation of duties. Each component is responsible for its own work making application more manageable. Second this way each component can be more testable. AngularJS provides first class support for unit testing. Thus we can quickly write tests for our services making them robust and less error prone.

And I must say that reference [1] is really a good article!

Reference

[1]http://viralpatel.net/blogs/angularjs-service-factory-tutorial/

Service and controller in angularJs的更多相关文章

  1. @Repository、@Service、@Controller 和 @Component

    转载:http://blog.csdn.net/ye1992/article/details/19971467 spring 自 2.0 版本开始,陆续引入了一些注解用于简化 Spring 的开发.@ ...

  2. Spring注解@Component、@Repository、@Service、@Controller区别 .

    Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository.@Service 和 @Controller.在目前的 Spring ...

  3. [转] Spring注解@Component、@Repository、@Service、@Controller区别

    原文地址:http://blog.csdn.net/zhang854429783/article/details/6785574 很长时间没做web项目都把以前学的那点框架知识忘光了,今天把以前做的一 ...

  4. @Repository、@Service、@Controller 和 @Component(转)

    鸣谢:http://blog.csdn.net/ye1992/article/details/19971467 @Repository.@Service.@Controller 和 @Componen ...

  5. Spring注解@Component、@Repository、@Service、@Controller,@Autowired、@Resource用法

    一.Spring定义bean,@Component.@Repository.@Service 和 @Controller Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥 ...

  6. 【转】Spring注解@Component、@Repository、@Service、@Controller区别

    http://blog.csdn.net/zhang854429783/article/details/6785574 很长时间没做web项目都把以前学的那点框架知识忘光了,今天把以前做的一个项目翻出 ...

  7. Spring中用@Component、@Repository、@Service和 @Controller等标注的默认Bean名称会是小写开头的非限定类名

    今天用调度平台去调用bean中的方法时,提示找不到bean.经查,发现是由于如果在标注上没有提供name属性值,则默认的bean名称是小写开头的,而不是大写开头的. 下面是其他文档参阅: 使用过滤器自 ...

  8. Spring学习使用标签来标记资源(@Component、@Repository、 @Service和@Controller)和用法(包括如何jsp正在使用)

    首先,在xml其中新增部分标有下划线的文件,容器初始化的时候需要扫描包 注意: a.     包款扫描(下划线部分)一定要加,默认是不扫描整个包.与每一包之间','开.如过具有同样的父包,那么我们能够 ...

  9. SpringMVC注解@Component、@Repository、@Service、@Controller区别

    SpringMVC中四个基本注解: @Component.@Repository   @Service.@Controller 看字面含义,很容易却别出其中三个: @Controller   控制层, ...

随机推荐

  1. 美团HD(1)-设置导航栏主题

    自定义一个UINavigationController DJNavigationController.h #import <UIKit/UIKit.h> @interface DJNavi ...

  2. 推荐几款我一直在用的chrome插件(上)

    我用的chrome插件挺多的,所谓工欲善其事必先利其器,我热衷于搜寻好用的工具来让我平时的工作事半功倍.下面介绍几款我正在用的感觉还不错的插件,如果大家还有其它好用的(肯定有,chrome插件库太庞大 ...

  3. CentOS 6 部署GlusterFS

    首先需要关闭CentOS的防火墙和selinux,否则glusterfs将可能无法正常工作. /etc/init.d/iptables status 会得到一系列信息,说明防火墙开着. /etc/in ...

  4. 硬盘安装linux的两条命令

    kernel (hd0,0)/vmlinuz boot=casper iso-scan/filename=/ubuntu-10.04.1-desktop-i386.iso ro quiet splas ...

  5. CJCMS系列---说说项目中的缓存实现(1)

    缓存者,临时文件交换区也.主要就是方便查找,提高查找效率(效率在于读内存速度比读硬盘快).  大多数的项目的缓存都是通过设定过期时间来做的,可是我对于这样的替换策略不以为然,而且会导致混乱. 有人说: ...

  6. CJCMS系列--demo代码篇

    CJCMS之前写了不多的文章,但是一直没有上代码,也许代码写得很乱很差,但是我觉得总得有个开始,持续改进.作为一个喜欢分享的程序员,我决定分享一下. 前面文章中的结构比较全,而此次我给出的demo,没 ...

  7. Java课程设计--山寨版QQ

    Java课设要求做一个聊天通讯窗口,索性直接照着QQ撸了一个Demo出来 界面高仿qq,初学JAVA,技术比较渣,不喜勿喷 线程开的比较多性能不是太好,一般电脑开两个客户端聊天就卡卡的 先上图 使用方 ...

  8. ORA-00257: archiver error. Connect internal only, until freed.

    早上BA抄送客户的邮件过来,说系统用不了,应用系统报异常Unable to open connection to oracle,Microsoft Provider v2.0.50727.42,既然是 ...

  9. 常用正则表达式整理[JavaScript]

    URL /^(http\:\/\/|https\:\/\/)(.{4,})$/ 18位身份证号 //前17位数字,末位支持X/^\d{17}\d{1}$|^\d{17}x{1}$/ 手机号 //11位 ...

  10. dell md3200i mdss (企业管理) 安装的那点事儿

    首先获取安装包,解压后如图: 我是在windows 机上安装,所以执行windows 文件夹下的可执行程序: 双击红箭头文件,进行安装,步骤截图如下: 出现最后这个界面,就说明安装成功,直接重启系统就 ...